We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Loops in templates

Development and Programming

Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
17 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn

The only way of putting repetative numerical values into 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.

So i have developed a loop plugin that solves the problem by allowing you to perform loops in your templates. Its a free download here:

http://www.putyourlightson.net/projects/loop

       
Ingmar Greil's avatar
Ingmar Greil
29,243 posts
17 years ago
Ingmar Greil's avatar Ingmar Greil

Interesting, but shouldn’t that go in the Plugins forum?

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Hi Ben,

Great Add-On!

A few notes: 1) when using the plugin inside a weblog tag (or query tag), the {count} variable will show the weblog’s {count} variable, you could change yours to something like {loop_count} or {counter}. 2) you might also want to start prefixing your EE Add-Ons 😊

Now let’s see if we can use this plugin to easily create multiple columns of weblog entries :coolsmirk:

Cheers!

       
Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
17 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn

thanks for the suggestions. i’ve changed the file name to pi.for_loop.php and the variable to {loop_count}.

please feel free to post any cool uses of the plugin, i expect there might be a few!!

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Hi ben,

From the Developer Guidelines: (although I don’t think it is a requirement)

“Choose a prefix that is unique to your identity as a developer or company”

…so why not pi.bc_loop.php or pi.pylo_loop.php?

I just tested the plugin with some rating value (0 to 5), pretty cool.

{exp:loop start="0" end="{rating}" increment="1"}
star.gif
{/exp:loop}
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Ben,

Looks set to be a really great little add-on! Well done.

One little thing though and this is me just nitpicking but on your page it says “repetative numerical” instead of “repetitive numerical” 😉

Great plugin though and definitely one I will use so thanks for that.

Best wishes,

Mark

       
Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
17 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn
…so why not pi.bc_loop.php or pi.pylo_loop.php?

i think the file name and the exp tag should be intuitive so that it is easier to understand when seen in a template. for_loop actually describes it pretty well since three values (initial, limit, increment) have to be set, and i assume most people have heard of a for loop, but maybe not…?

@mark: that really is nitpicking but i’ll change it just for you!!

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen
@mark: that really is nitpicking but i’ll change it just for you!!

So shoot me 😉

Actually just kidding, don’t shoot me please!! I have just always been one to notice spelling mistakes and sometimes I really should just keep my g*b shut. Working in radio advertising it has become a staple part of everyday life for me unfortunately!

I’ll bet I’ve probably made some mistakes in this post anyway so will cower off into the corner now!!

Again, great plugin (shouldn’t this word really be plug-in?) though so thanks for that!

Best wishes,

Mark

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Ben,

The name being so intuitive is exactly why you should prefix it 😉

Again from the Guidlines:

When your class or filename is a common word, or might quite likely be identically named in another PHP script, provide a unique prefix to help prevent collision. Always realize that your end users may be running other add-ons or third party PHP scripts.

I’m not trying to annoy you, just hoping that all EE devs start prefix their add-ons with a personal prefix like the Guidelines propose. Most EE devs are already doing this…

*ducks and starts updating his own add-ons 😉

       
Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
17 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn

haha, ok i’ll consider it, thanks cocoa

cheers mark!

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen
cheers mark!

I’m sure you don’t really mean that! 😉 I can hear you scraping away at my Avatar on screen trying to gouge out my eyes!! 😊

Best wishes,

Mark

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Moved to the Plugins forum. Ben, after you’ve assembled the feedback and are happy with your plugin, don’t forget to submit it to the Add-On Library!

       
vosSavant's avatar
vosSavant
380 posts
16 years ago
vosSavant's avatar vosSavant

Ben,

Great stuff. I’ve come up against a problem, although it could be related to the way EE parses custom fields.

I want to display a list of testimonials, say 10 of them on a page. I’ve named each custom field testimonial_X, where X is the index of the for loop.

My code looks like this:

{exp:for_loop start="1" end="10" increment="1"}
    <li class="testimonial">
        {testimonial_{index}}
    </li>
{/exp:for_loop}

Instead of outputting the testimonial content, I just get:

{testimonial_1} {testimonial_2} {testimonial_3} etc…

Am I doing this right, or is what I’m trying to do not possible?

Thanks!

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi Ben,

I have an idea how to make Loop plugin more powerful.

Let’s consider an example. Say, there is “my_weblog” weblog in which there are used custom fields whose names are as follows:

custom_field_1
custom_field_2
custom_field_3
...
custom_field_49
custom_field_50

Now say the code as this is needed:

{exp:weblog:entries weblog="my_weblog" url_title="{segment_3}"}
{if custom_field_1 != ""}{custom_field_1}{/if}

{if custom_field_2 != ""}{custom_field_2}{/if}

{if custom_field_3 != ""}{custom_field_3}{/if}

...
{if custom_field_49 != ""}{custom_field_49}{/if}

{if custom_field_50 != ""}{custom_field_50}{/if}

{/exp:weblog:entries}

It is natural to use in such situation Loop plugin. But because of the parse order the code as this will not work:

{exp:weblog:entries weblog="my_weblog" url_title="{segment_3}"}
{exp:for_loop start="1" end="50" increment="1"}
{if custom_field_{index} != ""}{custom_field_{index}}{/if}

{/exp:for_loop} 
{/exp:weblog:entries}

This code will work:

{exp:for_loop start="1" end="50" increment="1" parse="inward"}
{exp:weblog:entries weblog="my_weblog" url_title="{segment_3}"}
{if custom_field_{index} != ""}{custom_field_{index}}{/if}

{/exp:weblog:entries}
{/exp:for_loop}

But this code will be inefficient since exp:weblog:entries will be initiated 50 times.

The problem could be solved by introducing optional variable pair - {loop_area}{/loop_area} - which would be used for marking portion of tagdata which should be iterated:

{exp:for_loop start="1" end="50" increment="1" parse="inward"}
{exp:weblog:entries weblog="my_weblog" url_title="{segment_3}"}
{loop_area}
{if custom_field_{index} != ""}{custom_field_{index}}{/if}

{/loop_area}
{/exp:weblog:entries}
{/exp:for_loop}

This way would be no performance loss since exp:weblog:entries would be initiated only once.

If you would be interested in such enhancement of your plugin, I can do the necessary coding.

       
vosSavant's avatar
vosSavant
380 posts
16 years ago
vosSavant's avatar vosSavant

I’ve since come across the LG Data Matrix extension, which renders many loop situations moot. I’d strongly recommend anyone check into this one.

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.