简体   繁体   中英

Assign new variable to smarty while in {php}

Currently I am in a template and need {php} to read something from the Model/Database. This shall now be a new smarty variable within the current template. How do I solve this?

eg:

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

The value is {$newSmartyVar} !

What's the correct code here?

(Yee, uncommon and not abstract but only needed for rapid prototyping. The code will go in the Controller later on.)

$this refers to the current smarty instance:

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

you should, however, avoid {php} like the plague. Using {php} is a sign of missing abstraction. You could look into creating a function plugin instead.

{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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM