I’m building a site at the moment that uses the Smarty templating engine and I came across a snag today. The global “Smarty” variable includes a nice $Smarty.post reference where by you can get posted variables to display in your template.
For example, if you have an input with the name of “firstname”, you could populate it with the posted item if you so wished. Like this:
1 |
<input type="text" name="firstname" value="{$smarty.post.firstname}" /> |
This is great if you have static input names.
From time to time though, you might be constructing your input names, like this:
1 |
<input type="text" name="firstname_{$question-/>id}" value="" /> |
I do this quite often for certain scenarios so that I know that the id can then be stripped off and processed correctly in the controller / processing function with PHP in the back end.
Something like the following doesn’t work in Smarty:
1 |
<input type="text" name="firstname" value="{$smarty.post.question_$question-/>id}" /> |
Instead you need to assign a new variable to be name you want, and then use that instead, like so:
1 2 |
{assign var="temp" value="question_`$question->id`"} <input type="text" name="firstname" value="{$smarty.post.$temp}" /> |
Image Credit: Jef Safi, 'pictosophizing
Pingback: Dynamic Variable Name In Smarty 2 | Muhammad Panji