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

Smarty 3: {for} tag

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Feb 08, 2010 6:43 pm    Post subject: Smarty 3: {for} tag Reply with quote

Smarty 3 now has an incredibly useful {for} tag for easy looping with a simple, clean syntax. Example, looping 5 times:

Code:
{for $x = 1 to 5}
  {$x}
{/for}


output:

1
2
3
4
5

Here is another example stepping by 2:

Code:
{for $x = 1 to 10 step 2}
  {$x}
{/for}


output:

1
3
5
7
9

You can also use any valid expressions:

Code:
{for $x = abs($min-1) to count($foo)}
 ...
{/for}



Inside the loop the following special vars can be accessed:

Code:
$x@iteration = number of iteration
$x@total = total number of iterations
$x@first = true on first iteration
$x@last = true on last iteration


Last edited by mohrt on Thu Mar 17, 2011 6:48 pm; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Fri Mar 26, 2010 2:54 pm    Post subject: Reply with quote

Thanks! Just what I needed. Very Happy
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 -> Tips and Tricks 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