簡體   English   中英

在{php}中將新變量分配給smarty

[英]Assign new variable to smarty while in {php}

目前,我在模板中,需要{php}從Model / Database中讀取某些內容。 現在這將是當前模板中的新smarty變量。 我該如何解決?

例如:

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

The value is {$newSmartyVar} !

這里正確的代碼是什么?

(是的,不常見,不是抽象的,但僅用於快速原型制作。代碼稍后將在Controller中使用。)

$this指向當前的smarty實例:

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

但是,您應該避免{php}那樣的瘟疫。 使用{php}表示缺少抽象。 您可以考慮創建一個功能插件。

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

從模型中得到一些東西:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM