View previous topic :: View next topic |
Author |
Message |
stoowert Smarty n00b
Joined: 25 Apr 2004 Posts: 1
|
Posted: Sun Apr 25, 2004 11:00 am Post subject: How to find the length of an array? |
|
|
Hi All -
I have a smarty variable that has been assigned to an array in PHP:
Code: |
$myvar = array( 1, 2, 3, 4 );
|
How do I find the length of this array from within my smarty template?
I was hoping for something like
but I can't figure it out.
TIA,
Stew. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Apr 25, 2004 11:58 am Post subject: |
|
|
hi,
just use the php count() function as a modifier and prefix it with an @ to tell smarty to pass the input as an array:
{$myarray|@count}
hth |
|
Back to top |
|
NuAlpha Smarty Regular
Joined: 08 Jan 2004 Posts: 71 Location: US
|
Posted: Mon Jun 14, 2004 1:51 am Post subject: |
|
|
Can other PHP funtions be accessed from within Smarty templates in a similar manner? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Jun 14, 2004 3:01 am Post subject: |
|
|
Yes, as long as smarty's security is not enabled (which is the default) and if it is enabled, then you have to tell Smarty exactly which functions are to be allowed. Just remember that when you use a PHP function as a modifier, everything is passed in sequence as function parameters, starting with the value being modified. Eg:
Code: | {$haystack|strstr:$needle} |
Also note that PHP functions can be used directly in {if} clauses:
Code: | {if strstr($haystack, $needle)} .. {/if} |
|
|
Back to top |
|
NuAlpha Smarty Regular
Joined: 08 Jan 2004 Posts: 71 Location: US
|
Posted: Mon Jun 14, 2004 3:44 am Post subject: |
|
|
Where do I tell Smarty which functions to allow? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Jun 14, 2004 4:16 am Post subject: |
|
|
[php:1:7b482fbcac]$smarty->security_settings['MODIFIER_FUNCS'] = array('count', 'strstr', ...);[/php:1:7b482fbcac]
Again, this is only used when $smarty->security == true (and 'count' is provided by default)
The manual, of course, has more info: http://smarty.php.net/manual/en/variable.security.settings.php |
|
Back to top |
|
NuAlpha Smarty Regular
Joined: 08 Jan 2004 Posts: 71 Location: US
|
Posted: Mon Jun 14, 2004 3:00 pm Post subject: |
|
|
Thanks for making my Smarty life a little easier! I was afraid I was going to have to write a custom function for Smarty for each PHP function I wanted to access. _________________ Smarty -> v2.6.11
PHP -> v5.2.1 |
|
Back to top |
|
khan2265 Smarty Rookie
Joined: 07 May 2004 Posts: 21
|
Posted: Tue Jul 13, 2004 6:26 pm Post subject: |
|
|
<td width="25%" bgcolor="#C0C0C0" height="30" colspan="{$names|@count}"
this works but, for the colspan I want to do something like
colspan = ("{$names|@count}" + 2 )
but have no idea what the syntax should be
can someone help me |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Jul 13, 2004 6:39 pm Post subject: |
|
|
{math equation="x+2" x=$names|@count}
greetings |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jul 13, 2004 6:40 pm Post subject: |
|
|
hi khan2265,
you can not do that inline but you can do it with a temporary assign:
Code: | {assign var=names_count value=$names|@count}
<td width="25%" bgcolor="#C0C0C0" height="30" colspan="{$names_count+2}" |
Alternatively, if you had a modifier that could do math you could do it inline, but I don't know of any that are currently available. |
|
Back to top |
|
|