Re:Question about handling undefined variables

From: Date: Mon, 14 Jan 2002 14:19:27 +0000
Subject: Re:Question about handling undefined variables
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-2533@lists.php.net to get a copy of this message
On Fri, 11 Jan 2002, Dan Thompson wrote: > Thanks for all the help. > > I tried as suggested using > {if isset($description)} {$description} {/if} > which worked as needed. > > I then modified it to > {if isset($description)} {$description} {else} &nbsp; {/if} > which also worked as needed. > > I then proceeded seeing if I could it as a custom function (I think what > would serve this best though is custom modifier. Defined as such based on > default: > function smarty_mod_isset($string, $default="") > { > if (!isset($string) || empty($string)) > return $default; > else > return $string; > } > > This is then registered as > $smarty->register_modifier("isset", "smarty_mod_isset"); > > Called from template as: > {$description|isset} > > Should also produce the same results with cleaner templates than all the > if's. But I am receiving the same error message about undefined index: > description. > > Can anybody offer any potential next steps on this? Try this patch against the current snapshot: --- Smarty_Compiler.class.php 2001/12/28 16:53:33 1.92 +++ Smarty_Compiler.class.php 2002/01/14 14:18:10 @@ -213,5 +213,5 @@ settype($tag_command, 'array'); $this->_parse_vars_props($tag_command); - return "<?php echo $tag_command[0]; ?>\n"; + return "<?php if (isset($tag_command[0])) echo $tag_command[0]; ?>\n"; } Be warned that this will slow-down the variable output by some percentage. Feel free to determine what that percentage is and let us know too. :) -Andrei Work is of two kinds: first, altering the position of matter at or near the earth's surface relatively to other matter; second, telling other people to do so. The first kind is unpleasant and ill-paid, the second is pleasant and highly paid. -- Bertrand Russell

« previous php.smarty.general (#2533) next »