File Coverage

blib/lib/Template/Liquid/Tag/Increment.pm
Criterion Covered Total %
statement 31 35 88.5
branch 8 16 50.0
condition 5 9 55.5
subroutine 8 9 88.8
pod 0 2 0.0
total 52 71 73.2


line stmt bran cond sub pod time code
1             our $VERSION = '1.0.22';
2             use strict;
3 24     24   161 use warnings;
  24         43  
  24         554  
4 24     24   101 require Template::Liquid::Error;
  24         38  
  24         787  
5             require Template::Liquid::Utility;
6             use base 'Template::Liquid::Tag';
7 24     24   115  
  24         47  
  24         13828  
8 24     24   71 my ($class, $args) = @_;
9             raise Template::Liquid::Error {type => 'Context',
10             template => $args->{template},
11 12     12 0 24 message => 'Missing template argument',
12             fatal => 1
13             }
14             if !defined $args->{'template'};
15             raise Template::Liquid::Error {type => 'Context',
16             template => $args->{template},
17 12 50       26 message => 'Missing parent argument',
18             fatal => 1
19             }
20             if !defined $args->{'parent'};
21             raise Template::Liquid::Error {
22             type => 'Syntax',
23 12 50       25 template => $args->{template},
24             message => 'Unused argument list in ' . $args->{'markup'},
25             fatal => 1
26             }
27             if defined $args->{'attrs'} && $args->{'attrs'} !~ m[\S$]o;
28             my ($name, $s);
29             if ($args->{'attrs'} =~ m[^\s*(.+?)\s*\:\s*(.*)$]o) { # Named syntax
30 12 50 33     56 ($name, $args->{'attrs'}) = ($1, $2);
31 12         19 $name = $2 if $name =~ m[^(['"])(.+)\1$];
32 12 50       170 }
    50          
33 0         0 elsif ($args->{'attrs'} =~ m[^(.+)$]o) { # Simple syntax
34 0 0       0 $name = $args->{'attrs'};
35             }
36             else {
37 12         24 raise Template::Liquid::Error {
38             template => $s->{template},
39             type => 'Syntax',
40             message =>
41             sprintf(
42             q[Syntax Error in '%s %s' - Valid syntax: %s [name]],
43             $args->{'tag_name'}, $args->{'attrs'}, $class->_me()
44             ),
45             fatal => 1
46 0         0 };
47             }
48              
49             #$name = $args->{'tag_name'} . '-' . $name;
50             if (defined $args->{'template'}{document}->{'_INCREMENTS'}{$name}) {
51             $s = $args->{'template'}{document}->{'_INCREMENTS'}{$name};
52             }
53 12 100       28 else {
54 8         14 $s = bless {name => $name,
55             blocks => [],
56             tag_name => $args->{'tag_name'},
57             add => $class->_direction(),
58             template => $args->{'template'},
59             parent => $args->{'parent'},
60             markup => $args->{'markup'},
61             value => $class->_initial()
62             }, $class;
63 4         17 $args->{'template'}{document}->{'_INCREMENTS'}{$name} = $s;
64             }
65             return $s;
66 4         13 }
67              
68 12         24 1;
69             }
70 2     2   10  
71             my ($s) = @_;
72             my $name = $s->{template}{context}->get($s->{'name'}) || $s->{'name'};
73 2     2   6 $s = $s->{template}{document}->{'_INCREMENTS'}{$name} || $s;
74             my $node = $s->{'value'};
75 0     0   0 my $return
76             = ref $node ? $node->render() : $s->{template}{context}->get($node);
77             $s->{'value'} += $s->{'add'};
78 12     12 0 21 return $return;
79 12   66     30 }
80 12   66     33 1;
81 12         18  
82             =pod
83 12 50       33  
84 12         23 =encoding UTF-8
85 12         25  
86             =begin stopwords
87              
88             Lütke jadedPixel
89              
90             =end stopwords
91              
92             =head1 NAME
93              
94             Template::Liquid::Tag::Increment - Document-level Persistant Number
95              
96             =head1 Description
97              
98             Creates a new number variable, and increases its value by one every time it is
99             called. The initial value is C<0>.
100              
101             =head1 Synopsis
102              
103             {% increment my_counter %}
104             {% increment my_counter %}
105             {% increment my_counter %}
106              
107             ...will result in...
108              
109             0
110             1
111             2
112              
113             =head1 Notes
114              
115             Variables created through the C<increment> tag are independent from variables
116             created through assign or capture.
117              
118             In the example below, a variable named "var" is created through assign. The
119             C<increment> tag is then used several times on a variable with the same name.
120             Note that the C<increment> tag does not affect the value of "var" that was
121             created through C<assign>.
122              
123             {% assign var = 10 %}
124             {% increment var %}
125             {% increment var %}
126             {% increment var %}
127             {{ var }}
128              
129             ...would print...
130              
131             0
132             1
133             2
134             10
135              
136             =head1 See Also
137              
138             Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
139              
140             =head1 Author
141              
142             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
143              
144             The original Liquid template system was developed by jadedPixel
145             (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
146              
147             =head1 License and Legal
148              
149             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
150              
151             This program is free software; you can redistribute it and/or modify it under
152             the terms of The Artistic License 2.0. See the F<LICENSE> file included with
153             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
154             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
155              
156             When separated from the distribution, all original POD documentation is covered
157             by the Creative Commons Attribution-Share Alike 3.0 License. See
158             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
159             see http://creativecommons.org/licenses/by-sa/3.0/us/.
160              
161             =cut