Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to find the length of an array?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
stoowert
Smarty n00b


Joined: 25 Apr 2004
Posts: 1

PostPosted: Sun Apr 25, 2004 11:00 am    Post subject: How to find the length of an array? Reply with quote

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
Code:

{$myvar.length}


but I can't figure it out.

TIA,
Stew.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 25, 2004 11:58 am    Post subject: Reply with quote

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
View user's profile Send private message
NuAlpha
Smarty Regular


Joined: 08 Jan 2004
Posts: 71
Location: US

PostPosted: Mon Jun 14, 2004 1:51 am    Post subject: Reply with quote

Can other PHP funtions be accessed from within Smarty templates in a similar manner?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Jun 14, 2004 3:01 am    Post subject: Reply with quote

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
View user's profile Send private message
NuAlpha
Smarty Regular


Joined: 08 Jan 2004
Posts: 71
Location: US

PostPosted: Mon Jun 14, 2004 3:44 am    Post subject: Reply with quote

Where do I tell Smarty which functions to allow?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Jun 14, 2004 4:16 am    Post subject: Reply with quote

[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
View user's profile Send private message
NuAlpha
Smarty Regular


Joined: 08 Jan 2004
Posts: 71
Location: US

PostPosted: Mon Jun 14, 2004 3:00 pm    Post subject: Reply with quote

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. Embarassed
_________________
Smarty -> v2.6.11
PHP -> v5.2.1
Back to top
View user's profile Send private message
khan2265
Smarty Rookie


Joined: 07 May 2004
Posts: 21

PostPosted: Tue Jul 13, 2004 6:26 pm    Post subject: Reply with quote

<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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jul 13, 2004 6:39 pm    Post subject: Reply with quote

{math equation="x+2" x=$names|@count}

greetings
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jul 13, 2004 6:40 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP