Assign a new smarty variable while in {php}

I am currently in a template and need {php} to read something from a model / database. Now it will be a new flexible variable in the current template. How to solve this?

eg:.

{php} $var["newSmartyVar"] = model_gimme_data(); $currentTemplate->assign($var); {/php} The value is {$newSmartyVar} ! 

What is the correct code here?

(Yee, unusual, not abstract, but needed only for rapid prototyping. The code will later appear in the controller.)

+4
source share
2 answers

$this refers to the current smarty instance:

 {php} $this->assign('foo', 'bar'); {/php} {$foo} 

you must, however, avoid {php} like the plague. Using {php} is a sign of lack of abstraction. You might consider creating a function plugin.

+2
source
 {php} global $currentTemplate; $var = model_gimme_data(); $currentTemplate->assign('newSmartyVar',$var); {/php} 

to get something from the model:

 {php} global $currentTemplate; $this->_tpl_vars['variableName']; {/php} 
0
source

Source: https://habr.com/ru/post/1432016/


All Articles