Smarty Eval Function

I was wondering how I would parse the value of a php variable to a smarty-variable inside a php-string… still get it?Here’s an example of what I want:

//includes...

$smarty = new Smarty();

//in this string, I wanna put a name
$string = "My name is {\$name}.";

//the smarty var 'name' should be put inside the string above
$smarty->assign("name", "Chuck Norris");

//then I want to put it in my template
$smarty->assign("text", $string);

$smarty->display("test.tpl")

test.tpl:

{$text}

Now this is how my output looks:

My name is {$name}.

As you see, the name I wanted to parse in the string is not there yet. So this is where the {eval} functionality comes in handy! So lets put it to use:

In test.tpl we now use the {eval} function:

{eval var=$text}

Now let’s take a look at the output:

My name is Chuck Norris.

Allright! As you can see, I succesfully got the name in the string where I wanted it! You can use this technique in many situations, for example: Imagine having a database that contains HTML-code in which you want to parse some values, you can use {eval} to get the values in without a hassle!

Leave a Reply

Your email address will not be published. Required fields are marked *