Loop Plugin
This is an Expression Engine plugin that allows you to perform loops within any template without having to enable php.
Support can be found in the comments and in this EE forum thread.
Current Version: 1.1 (released 28 April 2008)
* Works with ExpressionEngine 1.6 and higher.
Concept
The only way of putting repetitive numerical values into your templates is either by hard-coding them in or by enabling and using php to do it. I always try to avoid enabling php in templates for security purposes, and hard-coding in for example a drop-down select box with 100 values is never fun.
This plugin solves the problem by allowing you to perform loops in your templates.
How It Works
The loop plugin works just like a simplified for loop does. It takes as parameters an initial value, an increment value, and a limit.
Here is an example of using the loop plugin to create a drop-down select box that allows the user to select the year in which they were born:
<select name="year">
<option value="">Select year</option>
{exp:for_loop start="1940" end="2008" increment="1"}
<option value="{index}">{index}</option>
{/exp:for_loop}
</select>
Produces:
The start parameter is optional and represents the starting value of the index. The default value is 1.
The end parameter is optional and represents the end value or limit of the index. The default value is 3.
The increment parameter is optional and represents the incremental value of the index on each iteration of the loop. The default value is 1.
The index variable is used to get the value of the index that is incremented in each iteration of the loop.
The loop_count variable can be used to get the number of times that the loop has been iterated over.
Another example that simply produces an arithmetic series:
{exp:for_loop start="0" end="99" increment="3"}
{if {loop_count} != 1},{/if} {index}
{/exp:for_loop}
Produces:
0 , 3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 , 27 , 30 , 33 , 36 , 39 , 42 , 45 , 48 , 51 , 54 , 57 , 60 , 63 , 66 , 69 , 72 , 75 , 78 , 81 , 84 , 87 , 90 , 93 , 96 , 99
This example shows how decrementing is possible:
{exp:for_loop start="2008" end="2000" increment="-1"}
{if {loop_count} != 1},{/if} {index}
{/exp:for_loop}
Produces:
2008 , 2007 , 2006 , 2005 , 2004 , 2003 , 2002 , 2001 , 2000
Installation
- Download and unzip loop.zip
- Upload pi.for_loop.php to the system/plugins folder.
Download
You can download and use the plugin for free here:
If this plugin has helped you and you are feeling generous then feel free to make a small donation, it is much appreciated:
Changelog
Version 1.1
- Added functionality to decrement







Comments
Rick
29 March 2008
Handy little plugin. Thanks! A cool added feature would be to set a variable for end such as end="current_year" That would make the above code timeless. Just an idea that came to mind...
Ben
29 March 2008
why you already can rick:
end="{current_time format="%Y"}"
Rick
29 March 2008
Ha! I was thinking that the variable would not be processed... very nice. :-)
Mark Bowen
28 April 2008
Hi Ben,
Just wanted to say great little plugin. Don't suppose you could add in a sort="asc" / sort="desc" type of thing though perhaps? ;-)
Would be great to enable items to come out in reverse order if needed.
Great little plugin though so thanks.
Best wishes,
Mark
rqdesign
28 April 2008
Nice plug in. Is it possible to decrease the value rather then increase. Say for example go from 2008 down to 2002. When I try I can only get it working from 2002 - 2008, if I try the other way around nothing appears.
Ben
28 April 2008
hi guys, thanks for the suggestions. here is version 1.1 with a decrement functionality.
see the example above and enjoy!!
Mark Bowen
28 April 2008
Hi Ben,
Fantastic!! Haven't tried it yet but will do in a little while. Absolutely brilliant support!! ;-)
Best wishes,
Mark
Ben
28 April 2008
what can i say, you got me at a good time!
Pie Man
17 June 2008
Hey Ben,
Great plugin!! :)
Just curious though... I haven't been able to get this to decrement.
Is there a "decement" parameter I need to specify?
I currently have it start at a higher number, and end at a lower number... no matter what I've tried, it will not show anything.
Thank you!
Pie Man
17 June 2008
Nevermind... I'm an idiot!
I could have sworn I combed through the Docs several times, but then it hit me :P
-1 ...nice!
Thanks!
Nathan Pitman
16 July 2008
Interestingly this doesn't seem to work when used inside of a user edit form from the Solspace 'User' module. Works fine outside of the user module tags, but not inside... I will see what I can work out...
Darren Lambert
30 July 2008
Great plugin, thanks. Shame it doesn't work inside the Solspace User module.
Ben
30 July 2008
i'm afraid its an issue with the user module, so there's nothing i can do.
Leave A Comment