Loop Plugin
*** Update: now compatible with EE2.0
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.2 (released 19 February 2010)
* Works with ExpressionEngine 2.0 and ExpressionEngine 1.6
1. 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.
2. 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
3. Installation
Download and unzip loop.zip, then follow the steps below.
For EE2.0:
- Open the loop_2_0 folder
- Upload the entire for_loop folder to the system/expressionengine/third_party/ folder
For EE1.6:
- Open the loop_1_6 folder
- Upload pi.for_loop.php to the system/plugins folder
4. 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:
5. Changelog
Version 1.2- Converted to an EE 2.0 compatible plugin (both versions available in download)
- Added functionality to decrement


6. 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.
Beebs
04 March 2009
Is it possible to loop month, but echo them not as a number but as a string January, February, ...etc
Ben
04 March 2009
Beebs: you could do it if you first put the month names into an array and then loop through the array. you'd have to do this with php though and i think it would be far easier to just list the months manually.
Beebs
04 March 2009
Yeah, that's what I was thinking before. The reason I like your module is that I do not have to make my template as php enabled.
But thanks for the reply. I think you are very generous and thanks a lot for that.
Great works.
Todd
07 October 2009
I'm having some trouble with the {if {loop_count} != 1},{/if} portion of the code. For some reason the if statement is not parsing but rather gets displayed to the screen. If I remove the if statement... the loop works fine. Any thoughts on this? It's in an embedded template too.
chichilatte
30 January 2010
Wow thanks for this simple n elegant little plugin!
One feature i immediately needed was a "less than" rather than an "equals" comparison for the end parameter. I suppose, coming from a coding background, i usually start my index at 0 rather than 1, so i need my loops to be 1 smaller. Ah who cares, it's great as it is :)
Sean
17 April 2010
Excellent plugin and does everything I want except for one thing. I'm trying to use the switch tag inside of the loop to change classes on an image and it's not working.
Ben
19 April 2010
hi sean, i'm afraid the switch tag is not supported in this plugin.
» View more comments
Leave A Comment