1

I have text this stored in my database:

Your email is: {$email} and your name is {$name}.

$text = get from db this field;

I read this field from the database

I assign with smarty this:

$smarty->assign('text', $text);
$smarty->assign($name, 'maria');
$smarty->assign($email, '[email protected]');

and visualize in my html page with this:

{$text}

Result is:

Your email is: {$email} and your name is {$name}.

My problem is Smarty not renders a variable inside a variable.

can someone help me?

3
  • 1
    Considering the purpose of templating is to separate the tasks (between people or in time). This approach is hard to read / find for the template designer. It takes away of the template's role and puts it in the hands of the logic or Controller. Commented Nov 7, 2013 at 12:47
  • @sofl's answer is correct in offering a direct solution to exactly what you asked. Please make sure you don't shoot yourself in the foot just because you can. Commented Nov 7, 2013 at 12:49
  • The "better approach" would be to either type the string in the template or if it absolutely needs to be separated then you need to create a sub-template file and include it. Commented Nov 7, 2013 at 12:50

2 Answers 2

1

You could do this

$smarty->assign('name', 'maria');
$smarty->assign('email', '[email protected]');
$smarty->assign('text',$smarty->fetch('string:'.$text));

check out http://www.smarty.net/docs/en/resources.string.tpl for more details

EDIT

If you store your templates in a database, I would recommend you to read about custom template resources http://www.smarty.net/docs/en/resources.custom.tpl

Sign up to request clarification or add additional context in comments.

Comments

1

Or if you use Smarty 2

$template = "Your email is: {$email} and your name is {$name}."

require_once( $smarty->_get_plugin_filepath( 'function', 'eval' ));
$smarty->assign('name', 'maria');
$smarty->assign('email', '[email protected]');

$compiled = smarty_function_eval(array('var'=>$template), $smarty);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.