简体   繁体   English

如何在运行模板时获取所有分配的 Smarty 变量?

[英]How to get all assigned Smarty variables while running the template?

I would like to get all variables assigned to Smarty inside the template.我想在模板中获取分配给 Smarty 的所有变量。 For example, if I have this code例如,如果我有这个代码

$smarty->assign('name', 'Fulano');
$smarty->assign('age', '22');
$result = $this->smarty->fetch("file:my.tpl");

I would like to have a my.tpl like the one below:我想要一个my.tpl ,如下所示:

{foreach from=$magic_array_of_variables key=varname item=varvalue}
{$varname} is {$varvalue}
{/foreach}

such as the content of result would be例如结果的内容是

name is Fulano
age is 22

So, is there a way to get this $magic_array_of_variables ?那么,有没有办法得到这个$magic_array_of_variables

all code below is smarty2 以下所有代码都是smarty2

All smarty variables are held inside the $smarty->_tpl_vars object, so before fetching() your template, you could do: 所有智能变量都保存在$ smarty - > _ tpl_vars对象中,因此在获取()模板之前,您可以执行以下操作:

$smarty->assign('magic_array_of_variables', $smarty->_tpl_vars);

Since this may be impractical, you could also write a small smarty plugin function that does something similar: 由于这可能不切实际,您还可以编写一个小巧的插件函数,它可以执行类似的操作:

function smarty_function_magic_array_of_variables($params, &$smarty) {
    foreach($smarty->_tpl_vars as $key=>$value) {
        echo "$key is $value<br>";
    }
}

and from your tpl call it with: 从你的tpl调用它:

{magic_array_of_variables}

Alternatively, in this function you can do: 或者,在此功能中,您可以:

function smarty_function_magic_array_of_variables($params, &$smarty) {
    $smarty->_tpl_vars['magic_array_of_variables'] =  $smarty->_tpl_vars;
}

and in your template: 并在您的模板中:

{magic_array_of_variables}
{foreach from=$magic_array_of_variables key=varname item=varvalue}
{$varname} is {$varvalue}
{/foreach}

You can just assign an array to your smarty variable. 您只需将数组分配给smarty变量即可。 Something like this: 像这样的东西:

$array = array('name' => 'Fulano', 'age' => '22');

when you assign this to your template with the name magic_array_of_variables , the exact smarty template you provided should give the output you want 当您使用名称magic_array_of_variables将其分配给模板时,您提供的确切magic_array_of_variables模板应该提供您想要的输出

There is no native way to iterate the assigned variables. 没有本地方法来迭代分配的变量。 That said, getTemplateVars() returns an associative array of all assigned values. 也就是说, getTemplateVars()返回所有赋值的关联数组。

Like @perikilis described, you can simply register a plugin function to push the result of getTemplateVars() back to the assigned variables list. 与@perikilis描述的一样,您只需注册一个插件函数即可将getTemplateVars()的结果推回到指定的变量列表中。 If you wanted to prevent some data duplication and other weirdness, you might want to only assign the array_keys() and access the actual variables like {${$varname}} (Smarty3). 如果您想要防止某些数据重复和其他奇怪现象,您可能只想分配array_keys()并访问实际变量,如{${$varname}} (Smarty3)。

Wonder if {debug} at the start of your tpl is that you need.想知道您的 tpl 开头的 {debug} 是否是您需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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