blib/lib/Template/Liquid/Tag/For.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 88.3% 53 / 60 Conditioncond 74.0% 20 / 27 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 86.7% 163 / 188 CC 43 SCAR 28.8
CC
43
Cov
91%
render
26.3
__ANON__
10.0
new
9.6
CRAP 17.8
« blib/lib/Template/Liquid/Context.pm ↑ Summary blib/lib/Template/Liquid/Condition.pm »
1 package Template::Liquid::Tag::For;
2 24 our $VERSION = '1.0.22';
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 require Template::Liquid::Error;
6 24 require Template::Liquid::Utility;
7 24 ▶use base 'Template::Liquid::Tag::If';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 my $Help_String = 'TODO';
9 24 ▶sub import { Template::Liquid::register_tag('for') }
Subroutine import: called CC=1 SCAR=0.0
10  
11 sub new {
12 103 ▶ my ($class, $args) = @_;
Subroutine new: called CC=9 SCAR=22.6
Pod undocumented
13 raise Template::Liquid::Error {type => 'Context',
14 template => $args->{template},
15 message => 'Missing template argument',
16 fatal => 1
17 }
18 103 ▶ if !defined $args->{'template'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"template"} 0 103 103
19 raise Template::Liquid::Error {type => 'Context',
20 template => $args->{template},
21 message => 'Missing parent argument',
22 fatal => 1
23 }
24 103 ▶ if !defined $args->{'parent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"parent"} 0 103 103
25 raise Template::Liquid::Error {
26 type => 'Syntax',
27 template => $args->{template},
28 message => 'Missing argument list in ' . $args->{'markup'},
29 fatal => 1
30 }
31 103 ▶ if !defined $args->{'attrs'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"attrs"} 0 103 103
32 103 ▶ if ($args->{'attrs'} !~ qr[^([\w\.]+)\s+in\s+(.+?)(?:\s+(.*)\s*?)?$]o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"attrs"} =~ qr/^([\w\.]+)\s+in\s+(.+?)(?:\s+(.*)\s*?)?$/o) 0 103 103
33 raise Template::Liquid::Error {
34 template => $args->{template},
35 type => 'Syntax',
36 0 message => 'Bad argument list in ' . $args->{'markup'},
37 fatal => 1
38 };
39 }
40 103 ▶ my ($var, $range, $attr) = ($1, $2, $3 || '');
Condition100%2/2
$3 || ''
l!l
5449
MC/DC100%1/1
$3 || ''
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
41 103 ▶ my $reversed = $attr =~ s[^reversed\b][]o ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ s/^reversed\b//o ? : 14 89 103
42 my %attr = map {
43  
44 #my $blah = $_;
45 #my ($k, $v)
46 # = grep { defined $_ }
47 # $_
48 # =~ m[$Template::Liquid::Utility::VariableFilterArgumentParser]g;
49 #use Data::Dump;
50 ##ddx [$k, $v];
51 72 my ($k, $v) = $_ =~ m[$Template::Liquid::Utility::TagAttributes]g;
52 72 { $k => $v };
53 103 ▶ } grep { defined && length } split qr[\s+]o, $attr || '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 83 0 83
Condition100%2/2
$attr || ''
l!l
5152
MC/DC100%1/1
$attr || ''
$attr
Truth table100%2/2
A
A= $attr
Aresult
11
00
54 my $s = bless {attributes => \%attr,
55 collection_name => $range,
56 name => $var . '-' . $range,
57 blocks => [],
58 conditional_tag => 'else',
59 reversed => $reversed,
60 tag_name => $args->{'tag_name'},
61 variable_name => $var,
62 end_tag => 'end' . $args->{'tag_name'},
63 template => $args->{'template'},
64 parent => $args->{'parent'},
65 103 markup => $args->{'markup'}
66 }, $class;
67 103 return $s;
68 }
69  
70 sub render {
71 105 ▶ my ($s) = @_;
Subroutine render: called CC=26 SCAR=32.7
Pod undocumented
72 105 my $range = $s->{'collection_name'};
73 105 my $attr = $s->{'attributes'};
74 105 my $reversed = $s->{'reversed'};
75 my $sorted
76 = exists $attr->{'sorted'}
77 ? $s->{template}{context}->get($attr->{'sorted'}) ||
78 105 ▶ $attr->{'sorted'} ||
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'sorted'} ? : 14 91 105
Condition80%4/5
$s->{"template"}{"context"}->get($attr->{"sorted"}) or $attr->{"sorted"} or "key"
l!l
95
$s->{"template"}{"context"}->get($attr->{"sorted"}) or $attr->{"sorted"}
l!l&&r!l&&!r
095
MC/DC50%1/2
$s->{"template"}{"context"}->get($attr->{"sorted"}) or $attr->{"sorted"} or "key"
$s->{"template"}{"context"}->get($attr->{"sorted"}) $attr->{"sorted"}
Truth table66%2/3
A or B
A= $s->{"template"}{"context"}->get($attr->{"sorted"})B= $attr->{"sorted"}
ABresult
1X1
011
000
79 'key'
80 : ();
81 105 ▶ $sorted = 'key'
Branch: 1 branch
BranchTrueFalseTotal
if defined $sorted and ($sorted ne "key" and $sorted ne "value") 0 105 105
Condition66%4/6
defined $sorted and ($sorted ne "key" and $sorted ne "value")
!ll&&!rl&&r
91140
$sorted ne "key" and $sorted ne "value"
!ll&&!rl&&r
950
MC/DC0%0/3
defined $sorted and ($sorted ne "key" and $sorted ne "value")
defined $sorted $sorted ne "key" $sorted ne "value"
Truth table50%2/4
A and B and C
A= defined $sortedB= $sorted ne "key"C= $sorted ne "value"
ABCresult
0XX0
10X0
1100
1111
82 if (defined $sorted && (($sorted ne 'key') && ($sorted ne 'value')));
83 my $offset
84 = defined $attr->{'offset'}
85 105 ▶ ? $s->{template}{context}->get($attr->{'offset'})
Branch: 1 branch
BranchTrueFalseTotal
defined $attr->{'offset'} ? : 24 81 105
86 : ();
87 my $limit
88 = defined $attr->{'limit'}
89 105 ▶ ? $s->{template}{context}->get($attr->{'limit'})
Branch: 1 branch
BranchTrueFalseTotal
defined $attr->{'limit'} ? : 26 79 105
90 : ();
91 105 my $list = $s->{template}{context}->get($range);
92 105 my $type = 'ARRAY';
93  
94 #warn $list;
95 #
96 105 my $_undef_list = 0;
97 105 ▶ if (ref $list eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $list eq 'HASH') { } 16 89 105
elsif (defined $sorted) { } 3 86 89
98 16 $list = [map { {key => $_, value => $list->{$_}} } keys %$list];
99 @$list = sort {
100 16 ▶ $a->{$sorted} =~ m[^\d+$]o &&
Branch: 1 branch
BranchTrueFalseTotal
if defined $sorted 11 5 16
101 $b->{$sorted} =~ m[^\d+$]o
102 ? ($a->{$sorted} <=> $b->{$sorted})
103 29 ▶ : ($a->{$sorted} cmp $b->{$sorted})
Branch: 1 branch
BranchTrueFalseTotal
$Template::Liquid::Tag::For::a->{$sorted} =~ /^\d+$/o && $Template::Liquid::Tag::For::b->{$sorted} =~ /^\d+$/o ? : 11 18 29
Condition66%2/3
$Template::Liquid::Tag::For::a->{$sorted} =~ /^\d+$/o and $Template::Liquid::Tag::For::b->{$sorted} =~ /^\d+$/o
!ll&&!rl&&r
18011
MC/DC50%1/2
$Template::Liquid::Tag::For::a->{$sorted} =~ /^\d+$/o and $Template::Liquid::Tag::For::b->{$sorted} =~ /^\d+$/o
$Template::Liquid::Tag::For::a->{$sorted} =~ /^\d+$/o $Template::Liquid::Tag::For::b->{$sorted} =~ /^\d+$/o
Truth table66%2/3
A and B
A= $Template::Liquid::Tag::For::a->{$sorted} =~ /^\d+$/oB= $Template::Liquid::Tag::For::b->{$sorted} =~ /^\d+$/o
ABresult
0X0
100
111
104 } @$list if defined $sorted;
105 16 $type = 'HASH';
106 }
107 elsif (defined $sorted) {
108 @$list = sort {
109 3 ▶ $a =~ m[^\d+$] && $b =~ m[^\d+$] ? ($a <=> $b) : ($a cmp $b)
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^\d+$/ && $b =~ /^\d+$/ ? : 24 0 24
Condition33%1/3
$a =~ /^\d+$/ and $b =~ /^\d+$/
!ll&&!rl&&r
0024
MC/DC0%0/2
$a =~ /^\d+$/ and $b =~ /^\d+$/
$a =~ /^\d+$/ $b =~ /^\d+$/
Truth table33%1/3
A and B
A= $a =~ /^\d+$/B= $b =~ /^\d+$/
ABresult
0X0
100
111
110 } @$list;
111 }
112 105 ▶ if (!defined $list || !$list || !@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $list && $list && @$list) { } 2 103 105
Condition83%5/6
defined $list && $list && @$list
!ll&&!rl&&r
11103
defined $list and $list
!ll&&!rl&&r
10104
MC/DC66%2/3
defined $list && $list && @$list
defined $list $list @$list
Truth table75%3/4
A and B && C
A= defined $listB= $listC= @$list
ABCresult
0XX0
10X0
1100
1111
113 2 $_undef_list = 1;
114 2 $list = [1];
115 }
116 else { # Break it down to only the items we plan on using
117 103 ▶ my $min = (defined $offset ? $offset : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $offset ? : 24 79 103
118 103 ▶ my $max
Branch: 2 branches
BranchTrueFalseTotal
defined $limit ? : 26 77 103
defined $offset ? : 12 14 26
119 = (defined $limit
120 ? $limit + (defined $offset ? $offset : 0) - 1
121 : $#$list);
122 103 ▶ $max = $#$list if $max > $#$list;
Branch: 1 branch
BranchTrueFalseTotal
if $max > $list->$#* 4 99 103
123 103 $list = [@{$list}[$min .. $max]]
124 ; # make a copy so we can use the list again
125 103 ▶ @$list = reverse @$list if $reversed;
Branch: 1 branch
BranchTrueFalseTotal
if $reversed 14 89 103
126 103 ▶ $limit = defined $limit ? $limit : scalar @$list;
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 26 77 103
127 103 ▶ $offset = defined $offset ? $offset : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $offset ? : 24 79 103
128 }
129 return $s->{template}{context}->stack(
130 sub {
131 105 ▶ my $return = '';
Subroutine __ANON__: called CC=10 SCAR=23.0
132 105 my $steps = $#$list;
133 105 ▶ $_undef_list = 1 if $steps == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $steps == -1 8 97 105
134 105 my $nodes = $s->{'blocks'}[$_undef_list]{'nodelist'};
135 105 FOR: for my $index (0 .. $steps) {
136 $s->{template}{context}
137 384 ->set($s->{'variable_name'}, $list->[$index]);
138 $s->{template}{context}->set(
139 'forloop',
140 {length => $steps + 1,
141 limit => $limit,
142 offset => $offset,
143 384 ▶ name => $s->{'name'},
Branch: 2 branches
BranchTrueFalseTotal
$index == 0 ? : 97 287 384
$index == $steps ? : 93 291 384
144 first => ($index == 0 ? !!1 : !1),
145 last => ($index == $steps ? !!1 : !1),
146 index => $index + 1,
147 index0 => $index,
148 rindex => $steps - $index + 1,
149 rindex0 => $steps - $index,
150 type => $type,
151 sorted => $sorted
152 }
153 );
154 384 for my $node (@$nodes) {
155 1259 ▶ my $rendering = ref $node ? $node->render() : $node;
Branch: 1 branch
BranchTrueFalseTotal
ref $node ? : 449 810 1259
156 1259 ▶ $return .= defined $rendering ? $rendering : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $rendering ? : 1257 2 1259
157 1259 ▶ if ($s->{template}{break}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"template"}{"break"}) 4 1255 1259
158 4 $s->{template}{break} = 0;
159 4 last FOR;
160 }
161 1255 ▶ if ($s->{template}{continue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"template"}{"continue"}) 19 1236 1255
162 19 $s->{template}{continue} = 0;
163 19 next FOR;
164 }
165 }
166 }
167 105 return $return;
168 }
169 105 );
170 }
171 24 1;
172  
173 =pod
174  
175 =encoding UTF-8
176  
177 =begin stopwords
178  
179 Lütke jadedPixel iterable
180  
181 =end stopwords
182  
183 =head1 NAME
184  
185 Template::Liquid::Tag::For - Simple loop construct
186  
187 =head1 Synopsis
188  
189 {% for x in (1..10) %}
190 x = {{ x }}
191 {% endfor %}
192  
193 =head1 Description
194  
195 For loops... uh, loop over collections.
196  
197 =head2 Loop-scope Variables
198  
199 During every for loop, the following helper variables are available for extra
200 styling needs:
201  
202 =over
203  
204 =item * C<forloop.length>
205  
206 length of the entire for loop
207  
208 =item * C<forloop.index>
209  
210 index of the current iteration
211  
212 =item * C<forloop.index0>
213  
214 index of the current iteration (zero based)
215  
216 =item * C<forloop.rindex>
217  
218 how many items are still left?
219  
220 =item * C<forloop.rindex0>
221  
222 how many items are still left? (zero based)
223  
224 =item * C<forloop.first>
225  
226 is this the first iteration?
227  
228 =item * C<forloop.last>
229  
230 is this the last iteration?
231  
232 =item * C<forloop.type>
233  
234 are we looping through an C<ARRAY> or a C<HASH>?
235  
236 =back
237  
238 =head2 Attributes
239  
240 There are several attributes you can use to influence which items you receive
241 in your loop:
242  
243 =over
244  
245 =item C<limit:int>
246  
247 lets you restrict how many items you get.
248  
249 =item C<offset:int>
250  
251 lets you start the collection with the nth item.
252  
253 =back
254  
255 # array = [1,2,3,4,5,6]
256 {% for item in array limit:2 offset:2 %}
257 {{ item }}
258 {% endfor %}
259 # results in 3,4
260  
261 =head3 Reversing the Loop
262  
263 You can reverse the direction the loop works with the C<reversed> attribute. To
264 comply with the Ruby lib's functionality, C<reversed> B<must> be the first
265 attribute.
266  
267 {% for item in collection reversed %} {{item}} {% endfor %}
268  
269 =head3 Sorting
270  
271 You can sort the variable with the C<sorted> attribute. This is an extension
272 beyond the scope of Liquid's syntax and thus incompatible but it's useful.
273  
274 {% for item in collection sorted %} {{item}} {% endfor %}
275  
276 If you are sorting a hash, the values are sorted by keys by default. You may
277 decide to sort by values like so:
278  
279 {% for item in hash sorted:value %} {{item.value}} {% endfor %}
280  
281 ...or make the default obvious with...
282  
283 {% for item in hash sorted:key %} {{item.key}} {% endfor %}
284  
285 =head2 Numeric Ranges
286  
287 Instead of looping over an existing collection, you can define a range of
288 numbers to loop through. The range can be defined by both literal and variable
289 numbers:
290  
291 # if item.quantity is 4...
292 {% for i in (1..item.quantity) %}
293 {{ i }}
294 {% endfor %}
295 # results in 1,2,3,4
296  
297 =head2 Hashes
298  
299 To deal with the possibility of looping through hash references, I have chosen
300 to extend the Liquid Engine's functionality. When looping through a hash, each
301 item is made a single key/value pair. The item's actual key and value are in
302 the C<item.key> and C<item.value> variables. ...here's an example:
303  
304 # where var = {A => 1, B => 2, C => 3}
305 { {% for x in var %}
306 {{ x.key }} => {{ x.value }},
307 {% endfor %} }
308 # results in { A => 1, C => 3, B => 2, }
309  
310 The C<forloop.type> variable will contain C<HASH> if the looped variable is a
311 hashref. Also note that the keys/value pairs are left unsorted.
312  
313 =head2 C<else> tag
314  
315 The else tag allows us to do this:
316  
317 {% for item in collection %}
318 Item {{ forloop.index }}: {{ item.name }}
319 {% else %}
320 There is nothing in the collection.
321 {% endfor %}
322  
323 The C<else> branch is executed whenever the for branch will never be executed
324 (e.g. collection is blank or not an iterable or out of iteration scope).
325  
326 =for basis https://github.com/Shopify/liquid/pull/56
327  
328 =head1 TODO
329  
330 Since this is a customer facing template engine, Liquid should provide some way
331 to limit L<ranges|Template::Liquid::Tag::For/"Numeric Ranges"> and/or depth to
332 avoid (functionally) infinite loops with code like...
333  
334 {% for w in (1..10000000000) %}
335 {% for x in (1..10000000000) %}
336 {% for y in (1..10000000000) %}
337 {% for z in (1..10000000000) %}
338 {{ 'own' | replace:'o','p' }}
339 {%endfor%}
340 {%endfor%}
341 {%endfor%}
342 {%endfor%}
343  
344 =head1 See Also
345  
346 Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
347  
348 L<Template::Liquid|Template::Liquid/"Create your own filters">'s docs on custom
349 filter creation
350  
351 L<Template::Liquid::Tag::Break|Template::Liquid::Tag::Break> and
352 L<Template::Liquid::Tag::Continue|Template::Liquid::Tag::Continue>
353  
354 =head1 Author
355  
356 Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
357  
358 CPAN ID: SANKO
359  
360 =head1 License and Legal
361  
362 Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
363  
364 This program is free software; you can redistribute it and/or modify it under
365 the terms of L<The Artistic License
366 2.0|http://www.perlfoundation.org/artistic_license_2_0>. See the F<LICENSE>
367 file included with this distribution or L<notes on the Artistic License
368 2.0|http://www.perlfoundation.org/artistic_2_0_notes> for clarification.
369  
370 When separated from the distribution, all original POD documentation is covered
371 by the L<Creative Commons Attribution-Share Alike 3.0
372 License|http://creativecommons.org/licenses/by-sa/3.0/us/legalcode>. See the
373 L<clarification of the
374 CCA-SA3.0|http://creativecommons.org/licenses/by-sa/3.0/us/>.
375  
376 =cut
« blib/lib/Template/Liquid/Context.pm ↑ Summary blib/lib/Template/Liquid/Condition.pm »