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.

Download Loop Plugin
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:

  1. Open the loop_2_0 folder
  2. Upload the entire for_loop folder to the system/expressionengine/third_party/ folder

For EE1.6:

  1. Open the loop_1_6 folder
  2. 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)
Version 1.1
  • Added functionality to decrement

6. Comments

Blank Avatar
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...

Blank Avatar
Ben
29 March 2008


why you already can rick:

end="{current_time format="%Y"}"

Blank Avatar
Rick
29 March 2008


Ha! I was thinking that the variable would not be processed... very nice. :-)

Blank Avatar
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

Blank Avatar
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.

Blank Avatar
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!!

Blank Avatar
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

Blank Avatar
Ben
28 April 2008


what can i say, you got me at a good time!

Blank Avatar
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!

Blank Avatar
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!

Blank Avatar
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...


» View more comments


Leave A Comment

name (required)
email (required but will be kept completely private)
website