HP-Handy
view release on metacpan or search on metacpan
doc/jinja2_cheatsheet.EN.txt view on Meta::CPAN
{% endfor %}
loop special variable:
loop.index 1-based counter
loop.index0 0-based counter
loop.revindex reverse counter (1-based)
loop.revindex0 reverse counter (0-based)
loop.first true on first iteration
loop.last true on last iteration
loop.length total number of items
loop.odd true on odd-numbered iterations (1st, 3rd, ...)
loop.even true on even-numbered iterations (2nd, 4th, ...)
[ 8. Set ]
{% set x = 42 %}
{% set greeting = "Hello, " ~ name %}
{% set items = [1, 2, 3] %}
[ 9. Include ]
{% include "header.html" %}
lib/HP/Handy.pm view on Meta::CPAN
Loop variable C<loop>:
loop.index 1-based counter
loop.index0 0-based counter
loop.revindex reverse counter (1-based)
loop.revindex0 reverse counter (0-based)
loop.first true on first iteration
loop.last true on last iteration
loop.length total number of items
loop.odd true on odd iterations
loop.even true on even iterations
Dict iteration:
{% for key, value in mapping %}
{{ key }}: {{ value }}
{% endfor %}
=head2 Set
{% set x = 42 %}
t/0005-tags-for.t view on Meta::CPAN
###############################################################################
# Basic iteration
###############################################################################
# ok 1-6
is(r('{% for i in x %}{{ i }}{% endfor %}', {x=>['a','b','c']}), 'abc', 'for: string list');
is(r('{% for i in x %}{{ i }}{% endfor %}', {x=>[1,2,3]}), '123', 'for: integer list');
is(r('{% for i in x %}{{ i }},{% endfor %}',{x=>['a','b']}), 'a,b,', 'for: with comma separator');
is(r('{% for i in x %}{% endfor %}', {x=>[1,2,3]}), '', 'for: empty body');
is(r('{% for i in x %}{{ i }}{% endfor %}', {x=>[42]}), '42', 'for: single element');
is(r('{% for i in x %}x{% endfor %}', {x=>[1,2,3,4,5]}), 'xxxxx','for: 5 iterations');
###############################################################################
# loop.index / loop.index0
###############################################################################
# ok 7-10
is(r('{% for i in x %}{{ loop.index }}{% endfor %}', {x=>['a','b','c']}),
'123', 'loop.index: 1-based');
is(r('{% for i in x %}{{ loop.index0 }}{% endfor %}', {x=>['a','b','c']}),
'012', 'loop.index0: 0-based');
( run in 0.534 second using v1.01-cache-2.11-cpan-71847e10f99 )