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