Recently I needed to assign several variables into one smarty variable inside the template.
Here is the basic rules for the example:
- You have a text that says “Your registration expires in # days”;
- You want The text “in # days” to change to “today” if the # days is 0.
Here’s a way you can do this with smarty variables inside the template (whether you should do this in the template is not a discussion for this post).
1 2 3 4 5 6 7 8 9 |
{assign var="num_days" value="0"} {if $num_days != "0"} {assign var="days_left" value="`$in_days` `$days_until_expiry` `$days`"} /* $days_left = in 3 days */ {else} {assign var="days_left" value="`$today`"} /* $days_left = today */ {/if} |
The key here is that when you want to include a variable as part of the value of another variable you need to use value=”$in_days
“. Notice the ` before and after the variable name inside the double quotes. You can use this method to include variables inside value statements anywhere in smarty templates and make modifications of those included variables.
That helped a lot. Thanks!