Re: [SMARTY] Count array function/modifier -- does it exist?
| From: | Simon Willison | Date: | Fri, 26 Jul 2002 11:15:41 +0000 |
| Subject: | Re: [SMARTY] Count array function/modifier -- does it exist? | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5459@lists.php.net to get a copy of this message | ||
At 07:14 26/07/2002 +0700, Hendy Irawan wrote:
Hi all in smarty-general, I wonder whether a template function/modifier is available to count the number of items in an array. There modifiers to count words, paragraphs, etc. but to count arrays??? (yes, just a simple call to count($whaterever);) Thank you.Here's a quick plugin that should do what you want. Just save it as modifier.count_array.php in your Smarty plugins directory, then use {$array|count_array} to call it. I've added it to the Smarty Wiki as well: http://smarty.incutio.com/?page=SmartyPlugins Cheers, Simon Willison ==== <?php /* * Smarty plugin * -------------------------------------------------------------
* Type: modifier * Name: count_array* Purpose: count the number of elements in an array * ------------------------------------------------------------- */ function smarty_modifier_count_array($array) {
if (is_array($array)) {
return count($array);
} else {
return 0;
}
}
?>