RE: [SMARTY] Checking for nonexistent array keys in template
| From: | Gary Loescher | Date: | Wed, 04 Sep 2002 14:08:56 +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-5924@lists.php.net to get a copy of this message | ||
By the way, in_array searches for values, not keys.
If you want to look for keys, use array_key_exists
Gary
>-----Original Message-----
>From: Geranium [mailto:vlcc69jfbo001@sneakemail.com]
>Sent: Wednesday, September 04, 2002 3:50 AM
>To: smarty-general@lists.php.net
>Subject: [SMARTY] Checking for nonexistent array keys
>in template
>
>
>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.
>
>--
>Smarty General Mailing List (http://smarty.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>