如何检查是否已将特定值分配给Smarty,如果未分配(默认)值?
答案:
if ($this->cismarty->get_template_vars('test') === null) {
$this->cismarty->assign('test', 'Default value');
}
发布于 2008-12-08 08:59:15
Smarty 2
if ($smarty->get_template_vars('foo') === null)
{
$smarty->assign('foo', 'some value');
}
Smarty 3
if ($smarty->getTemplateVars('foo') === null)
{
$smarty->assign('foo', 'some value');
}
请注意,对于Smarty3,您将不得不使用$smarty->getTemplateVars
。
发布于 2008-12-08 09:27:39
如果您没有设置变量,get_template_vars()
将返回null,因此您可以这样做
if ($smarty->get_template_vars('test') === null) {
echo "'test' is not assigned or is null";
}
但是,如果您分配了一个变量但设置为null,则检查将失败,在这种情况下,您可以这样做
$tmp = $smarty->get_template_vars();
if (!array_key_exists('test', $tmp)) {
echo "'test' is not assigned";
}
发布于 2008-12-08 09:02:42
我很确定你可以这样做:
if (!isset($smarty['foo']))
{
$smarty->assign('foo', 'some value');
}
https://stackoverflow.com/questions/350129
复制