Checking for nonexistent array keys in template
| From: | Geranium | Date: | Wed, 04 Sep 2002 08:50:26 +0000 |
| Subject: | Checking for nonexistent array keys in template | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-5920@lists.php.net to get a copy of this message | ||
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}
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. Because I'm using it to turn the whole section on
and off, I can't just use the :default option.
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:
{if in_array("name", $chunkarray)}Hello {$chunkarray["name"]}!{/if}
I'm using this when generating lots of broadly similar emails from the
same template.