Re: [SMARTY] Checking for nonexistent array keys in template

From: Date: Wed, 04 Sep 2002 09:17:31 +0000
Subject: Re: [SMARTY] Checking for nonexistent array keys in template
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5921@lists.php.net to get a copy of this message
On 4 Sep 2002 at 10:50, Geranium wrote: > I have a template that references particular keys in an array to enable > and disable chunks of the template, like this: > > {if $chunkarray["name"] != NULL}Hello {$chunkarray["name"]}!{/if} That's not the official way to to it. Assoc. arrays are written like this in Smarty: $chunkarray.name I don't know if your example works and I also don't know if Smarty allows the NULL inside the if-tag. Why don't you write {if $chunkarray.name}Hello {$chunkarray.name}!{/if} - this should work although I did not test it. > If this array key doesn't exist, in plain PHP this generates a notice > (i.e. we can live with it), but in Smarty it's a fatal error and stops > the whole program. Are you sure? In my experience Smarty would suppress the notice and ignore it without generating a fatal error (otherwise the default modifier wouldn't be possible!!!) Please have a look at the PHP script Smarty generated from your template and see how it translated your line. Perhaps it produces a parsing error? > Because I'm using it to turn the whole section on > and off, I can't just use the :default option. {if $somevar|default:false} > > How should I work around this? Should I temporarily shell out to PHP in > my template (not nice)? It would seem useful if Smarty allowed me to > check whether keys are valid by mapping through to something like > in_array(). Perhaps like this: Well, it does :-) You may use PHP function inside the if tags! If you turned Smarty::security on, make sure that 'in_array' is in the Smarty::security_settings['IF_FUNCS'] array (it is by default). > {if in_array("name", $chunkarray)}Hello {$chunkarray["name"]}!{/if} Yes, that _is_ possible. > I'm using this when generating lots of broadly similar emails from the > same template. HTH, Ferdinand -- Ferdinand Beyer <fbeyer@clickhand.de>

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