Solution::Tag::For - Simple loop construct
{% for x in (1..10) %} x = {{ x }} {% endfor %}
Solution allows for loops over collections.
During every for loop, the following helper variables are available for extra styling needs:
forloop.length
length of the entire for loop
forloop.index
index of the current iteration
forloop.index0
index of the current iteration (zero based)
forloop.rindex
how many items are still left?
forloop.rindex0
how many items are still left? (zero based)
forloop.first
is this the first iteration?
forloop.last
is this the last iternation?
forloop.type
are we looping through an ARRAY or a HASH?
ARRAY
HASH
There are several attributes you can use to influence which items you receive in your loop:
limit:int
lets you restrict how many items you get.
offset:int
lets you start the collection with the nth item.
# array = [1,2,3,4,5,6] {% for item in array limit:2 offset:2 %} {{ item }} {% endfor %} # results in 3,4
You can reverse the direction the loop works with the reversed attribute. To comply with the Ruby lib's functionality, reversed must be the first attribute.
reversed
{% for item in collection reversed %} {{item}} {% endfor %}
Instead of looping over an existing collection, you can define a range of numbers to loop through. The range can be defined by both literal and variable numbers:
# if item.quantity is 4... {% for i in (1..item.quantity) %} {{ i }} {% endfor %} # results in 1,2,3,4
To deal with the possibility of looping through hash references, Solution extends the Liquid Engine's functionality. When looping through a hash, each item is made a single key/value pair. The item's actual key and value are in the item.key and item.value. ...here's an example:
item.key
item.value
# where var = {A => 1, B => 2, C => 3} { {% for x in var %} {{ x.key }} => {{ x.value }}, {% endfor %} } # results in { A => 1, C => 3, B => 2, }
The forloop.type variable will contain HASH if the looped variable is a hashref. Also note that the keys/value pairs are left unsorted.
Since this is a customer facing template engine, Liquid should provide some way to limit ranges and/or depth to avoid (functionally) infinite loops with code like...
{% for w in (1..10000000000) %} {% for x in (1..10000000000) %} {% for y in (1..10000000000) %} {% for z in (1..10000000000) %} {{ 'own' | replace:'o','p' }} {%endfor%} {%endfor%} {%endfor%} {%endfor%}
Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
Solution's docs on custom filter creation
Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
The original Liquid template system was developed by jadedPixel (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
Copyright (C) 2009 by Sanko Robinson <sanko@cpan.org>
This program is free software; you can redistribute it and/or modify it under the terms of The Artistic License 2.0. See the LICENSE file included with this distribution or http://www.perlfoundation.org/artistic_license_2_0. For clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
When separated from the distribution, all original POD documentation is covered by the Creative Commons Attribution-Share Alike 3.0 License. See http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification, see http://creativecommons.org/licenses/by-sa/3.0/us/.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'Lütke'. Assuming UTF-8
To install Solution, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Solution
CPAN shell
perl -MCPAN -e shell install Solution
For more information on module installation, please visit the detailed CPAN module installation guide.