从智能模板文件中读取使用过的变量

时间:2013-04-06 14:13:49

标签: php variables smarty3

在将变量分配给smarty对象之前,有没有办法从smarty(3)模板文件中获取所有使用过的变量?

例如,我有以下模板文件:

Hello {$user.firstname},<br />
You are active in the following groups:<br />
{foreach from=$user.groups item=group}
   - {$group.name}<br />
{/foreach}
<br />
The city you live in is: {$city}

现在我问的原因是因为我不想获取所有用户信息,而是根据smarty中使用的变量创建一个查询。

无论如何都要做以下事情:

$smarty = new smarty;

$result = $smarty->getVariablesFromTemplate('index.tpl');

print_r($result);

/* outputs:
array(
   'user' => array(
      'firstname' => true,
      'groups' => array(
         'name' => true
      ),
      'city' => true
   )
);
*/

1 个答案:

答案 0 :(得分:0)

我不确定您的SQL,但您可以通过以下代码访问分配给smarty的模板变量(使用$smarty->assign('var', 'value')

$tpl_vars = $smarty->_tpl_vars;