Liquid/Tag/For.pm

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