Search code examples
phpsmarty

How can I judge a variable whether is assign in Smarty template?


I have a smarty project. in the php file I will assign the variable bar dynamically.

in the php file's corresponding template:

{if $bar}{$bar}{/if}

but I will get bellow error:

Notice: Undefined index: bar in /Users/sof/Desktop/TestIOS/smarty-test02/templates_c/6f98597c1882f0124e0891c8343f1404eff83e24_0.file.test.tpl.cache.php on line 63

Notice: Trying to get property of non-object in /Users/sof/Desktop/TestIOS/smarty-test02/templates_c/6f98597c1882f0124e0891c8343f1404eff83e24_0.file.test.tpl.cache.php on line 63

How can I judge a variable whether is assign?


Solution

  • You can use PHP's isset function.

    {if isset($bar)}{$bar}{/if}