| 1 | package Template::Liquid::Tag::Increment; | ||||||||||||||||||||||||||||
| 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'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 25 | ▶ | sub import { Template::Liquid::register_tag('increment') } | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=25.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 12 | 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 | ▶ | if !defined $args->{'template'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | raise Template::Liquid::Error {type => 'Context', | ||||||||||||||||||||||||||||
| 19 | template => $args->{template}, | ||||||||||||||||||||||||||||
| 20 | message => 'Missing parent argument', | ||||||||||||||||||||||||||||
| 21 | fatal => 1 | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 12 | ▶ | if !defined $args->{'parent'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if defined $args->{'attrs'} && $args->{'attrs'} !~ m[\S$]o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"attrs"} and not $args->{"attrs"} =~ /\S$/o
MC/DC0%0/2
defined $args->{"attrs"} and not $args->{"attrs"} =~ /\S$/o
defined $args->{"attrs"}
not $args->{"attrs"} =~ /\S$/o
Truth table33%1/3
A and not(B)
A= defined $args->{"attrs"}B= not $args->{"attrs"} =~ /\S$/o
| |||||||||||||||||||||||||||||
| 31 | 12 | my ($name, $s); | |||||||||||||||||||||||||||
| 32 | 12 | ▶ | if ($args->{'attrs'} =~ m[^\s*(.+?)\s*\:\s*(.*)$]o) { # Named syntax | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 33 | 0 | ($name, $args->{'attrs'}) = ($1, $2); | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | $name = $2 if $name =~ m[^(['"])(.+)\1$]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | elsif ($args->{'attrs'} =~ m[^(.+)$]o) { # Simple syntax | ||||||||||||||||||||||||||||
| 37 | 12 | $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 | $args->{'tag_name'}, $args->{'attrs'}, $class->_me() | |||||||||||||||||||||||||||
| 47 | ), | ||||||||||||||||||||||||||||
| 48 | fatal => 1 | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #$name = $args->{'tag_name'} . '-' . $name; | ||||||||||||||||||||||||||||
| 53 | 12 | ▶ | if (defined $args->{'template'}{document}->{'_INCREMENTS'}{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 8 | $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 | markup => $args->{'markup'}, | |||||||||||||||||||||||||||
| 64 | value => $class->_initial() | ||||||||||||||||||||||||||||
| 65 | }, $class; | ||||||||||||||||||||||||||||
| 66 | 4 | $args->{'template'}{document}->{'_INCREMENTS'}{$name} = $s; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 12 | return $s; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 2 | ▶ | sub _initial {0} | ||||||||||||||||||||||||||
|
Subroutine
_initial: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub _direction { | ||||||||||||||||||||||||||
|
Subroutine
_direction: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 2 | 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub _me {'increment'} | ||||||||||||||||||||||||||
|
Subroutine
_me: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 12 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 12 | my ($s) = @_; | |||||||||||||||||||||||||||
| 79 | 12 | ▶ | my $name = $s->{template}{context}->get($s->{'name'}) || $s->{'name'}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$s->{'template'}{'context'}->get($s->{'name'}) || $s->{'name'}
MC/DC0%0/2
$s->{'template'}{'context'}->get($s->{'name'}) || $s->{'name'}
$s->{'template'}{'context'}->get($s->{'name'})
$s->{'name'}
Truth table66%2/3
A || B
A= $s->{'template'}{'context'}->get($s->{'name'})B= $s->{'name'}
| |||||||||||||||||||||||||||||
| 80 | 12 | ▶ | $s = $s->{template}{document}->{'_INCREMENTS'}{$name} || $s; | ||||||||||||||||||||||||||
|
Condition66%2/3
$s->{'template'}{'document'}{'_INCREMENTS'}{$name} || $s
MC/DC0%0/2
$s->{'template'}{'document'}{'_INCREMENTS'}{$name} || $s
$s->{'template'}{'document'}{'_INCREMENTS'}{$name}
$s
Truth table66%2/3
A || B
A= $s->{'template'}{'document'}{'_INCREMENTS'}{$name}B= $s
| |||||||||||||||||||||||||||||
| 81 | 12 | my $node = $s->{'value'}; | |||||||||||||||||||||||||||
| 82 | my $return | ||||||||||||||||||||||||||||
| 83 | 12 | ▶ | = ref $node ? $node->render() : $s->{template}{context}->get($node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 12 | $s->{'value'} += $s->{'add'}; | |||||||||||||||||||||||||||
| 85 | 12 | return $return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 25 | 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 | ||||||||||||||||||||||||||||