How do I check to see if a Smarty variable is already assigned?

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

How do I check to see if a particular value has already been assigned to Smarty and if not assign a (default) value?

Answer:

if ($this->cismarty->get_template_vars('test') === null) {    $this->cismarty->assign('test', 'Default value'); }

回答1:

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'); }

Note that for Smarty 3, you will have to use $smarty->getTemplateVars instead.



回答2:

get_template_vars() will return null if you haven't set a variable, so you can do

if ($smarty->get_template_vars('test') === null) {     echo "'test' is not assigned or is null"; }

However that check will fail if you have a variable assigned but set as null, in which case you could do

$tmp = $smarty->get_template_vars(); if (!array_key_exists('test', $tmp)) {     echo "'test' is not assigned"; }


回答3:

Pretty sure you can do:

if (!isset($smarty['foo']))  {     $smarty->assign('foo', 'some value'); }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!