| 1 | package Template::Liquid::Tag::Cycle; | ||||||||||||||||||||||||||||
| 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('cycle') } | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 17 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 17 | my ($class, $args) = @_; | |||||||||||||||||||||||||||
| 12 | raise Template::Liquid::Error {type => 'Context', | ||||||||||||||||||||||||||||
| 13 | template => $args->{template}, | ||||||||||||||||||||||||||||
| 14 | message => 'Missing template argument', | ||||||||||||||||||||||||||||
| 15 | fatal => 1 | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | 17 | ▶ | 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 | 17 | ▶ | if !defined $args->{'parent'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | raise Template::Liquid::Error { | ||||||||||||||||||||||||||||
| 25 | type => 'Syntax', | ||||||||||||||||||||||||||||
| 26 | template => $args->{template}, | ||||||||||||||||||||||||||||
| 27 | message => 'Missing argument list in ' . $args->{'markup'}, | ||||||||||||||||||||||||||||
| 28 | fatal => 1 | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 17 | ▶ | if !defined $args->{'attrs'} || $args->{'attrs'} !~ m[\S$]o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"attrs"} and $args->{"attrs"} =~ /\S$/o
MC/DC0%0/2
defined $args->{"attrs"} and $args->{"attrs"} =~ /\S$/o
defined $args->{"attrs"}
$args->{"attrs"} =~ /\S$/o
Truth table33%1/3
A and B
A= defined $args->{"attrs"}B= $args->{"attrs"} =~ /\S$/o
| |||||||||||||||||||||||||||||
| 31 | 17 | my ($name, $s); | |||||||||||||||||||||||||||
| 32 | 17 | ▶ | if ($args->{'attrs'} =~ m[^\s*(.+?)\s*\:\s*(.*)$]o) { # Named syntax | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 33 | 11 | ($name, $args->{'attrs'}) = ($1, $2); | |||||||||||||||||||||||||||
| 34 | 11 | ▶ | $name = $2 if $name =~ m[^(['"])(.+)\1$]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | elsif ($args->{'attrs'} =~ m[^(.+)$]o) { # Simple syntax | ||||||||||||||||||||||||||||
| 37 | 6 | $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: cycle [name :] var [, var2, var3 ...]], | ||||||||||||||||||||||||||||
| 46 | 0 | $args->{'tag_name'}, $args->{'attrs'} | |||||||||||||||||||||||||||
| 47 | ), | ||||||||||||||||||||||||||||
| 48 | fatal => 1 | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #$name = $args->{'tag_name'} . '-' . $name; | ||||||||||||||||||||||||||||
| 53 | # XXX - Cycle objects are stored in Template::Liquid::Document objects | ||||||||||||||||||||||||||||
| 54 | 17 | ▶ | if (defined $args->{'template'}{document}->{'_CYCLES'}{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 7 | $s = $args->{'template'}{document}->{'_CYCLES'}{$name}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | my @list | ||||||||||||||||||||||||||||
| 59 | 90 | = grep { defined $_ } | |||||||||||||||||||||||||||
| 60 | 10 | $args->{'attrs'} | |||||||||||||||||||||||||||
| 61 | =~ m[$Template::Liquid::Utility::VariableFilterArgumentParser]g; | ||||||||||||||||||||||||||||
| 62 | $s = bless {name => $name, | ||||||||||||||||||||||||||||
| 63 | blocks => [], | ||||||||||||||||||||||||||||
| 64 | tag_name => $args->{'tag_name'}, | ||||||||||||||||||||||||||||
| 65 | list => \@list, | ||||||||||||||||||||||||||||
| 66 | template => $args->{'template'}, | ||||||||||||||||||||||||||||
| 67 | parent => $args->{'parent'}, | ||||||||||||||||||||||||||||
| 68 | 10 | markup => $args->{'markup'}, | |||||||||||||||||||||||||||
| 69 | position => 0 | ||||||||||||||||||||||||||||
| 70 | }, $class; | ||||||||||||||||||||||||||||
| 71 | 10 | $args->{'template'}{document}->{'_CYCLES'}{$name} = $s; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 17 | return $s; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 28 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 28 | my ($s) = @_; | |||||||||||||||||||||||||||
| 78 | 28 | ▶ | 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'}
| |||||||||||||||||||||||||||||
| 79 | 28 | ▶ | $s = $s->{template}{document}->{'_CYCLES'}{$name} || $s; | ||||||||||||||||||||||||||
|
Condition66%2/3
$s->{'template'}{'document'}{'_CYCLES'}{$name} || $s
MC/DC0%0/2
$s->{'template'}{'document'}{'_CYCLES'}{$name} || $s
$s->{'template'}{'document'}{'_CYCLES'}{$name}
$s
Truth table66%2/3
A || B
A= $s->{'template'}{'document'}{'_CYCLES'}{$name}B= $s
| |||||||||||||||||||||||||||||
| 80 | 28 | my $node = $s->{'list'}[$s->{'position'}++]; | |||||||||||||||||||||||||||
| 81 | my $return | ||||||||||||||||||||||||||||
| 82 | 28 | ▶ | = ref $node ? $node->render() : $s->{template}{context}->get($node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 28 | ▶ | $s->{'position'} = 0 if $s->{'position'} >= scalar @{$s->{'list'}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 28 | return $return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 25 | 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =pod | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =begin stopwords | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Lütke jadedPixel | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =end stopwords | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 NAME | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Template::Liquid::Tag::Cycle - Document-level Persistant Lists | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 Description | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Often you have to alternate between different colors or similar tasks. | ||||||||||||||||||||||||||||
| 105 | L<Template::Liquid|Template::Liquid> has built-in support for such operations, | ||||||||||||||||||||||||||||
| 106 | using the C<cycle> tag. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 Synopsis | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | {% cycle 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 111 | {% cycle 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 112 | {% cycle 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 113 | {% cycle 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | ...will result in... | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | one | ||||||||||||||||||||||||||||
| 118 | two | ||||||||||||||||||||||||||||
| 119 | three | ||||||||||||||||||||||||||||
| 120 | one | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | If no name is supplied for the cycle group, then it’s assumed that multiple | ||||||||||||||||||||||||||||
| 123 | calls with the same parameters are one group. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | If you want to have total control over cycle groups, you can optionally specify | ||||||||||||||||||||||||||||
| 126 | the name of the group. This can even be a variable. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 129 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 130 | {% cycle 'group 2': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 131 | {% cycle 'group 2': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | ...will result in... | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | one | ||||||||||||||||||||||||||||
| 136 | two | ||||||||||||||||||||||||||||
| 137 | one | ||||||||||||||||||||||||||||
| 138 | two | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 Notes | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | The cycle tag is the only one which retains state between calls to render For a | ||||||||||||||||||||||||||||
| 143 | particular document. So... | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | use Template::Liquid; | ||||||||||||||||||||||||||||
| 146 | my $solution = Template::Liquid->parse(<<'TEMPLATE'); | ||||||||||||||||||||||||||||
| 147 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 148 | {% cycle 'group 2': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 149 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 150 | TEMPLATE | ||||||||||||||||||||||||||||
| 151 | print $solution->render() for 1..3; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | ...would print... | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | one | ||||||||||||||||||||||||||||
| 156 | one | ||||||||||||||||||||||||||||
| 157 | two | ||||||||||||||||||||||||||||
| 158 | three | ||||||||||||||||||||||||||||
| 159 | two | ||||||||||||||||||||||||||||
| 160 | one | ||||||||||||||||||||||||||||
| 161 | two | ||||||||||||||||||||||||||||
| 162 | three | ||||||||||||||||||||||||||||
| 163 | three | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | ...rather than... | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | one | ||||||||||||||||||||||||||||
| 168 | one | ||||||||||||||||||||||||||||
| 169 | two | ||||||||||||||||||||||||||||
| 170 | one | ||||||||||||||||||||||||||||
| 171 | one | ||||||||||||||||||||||||||||
| 172 | two | ||||||||||||||||||||||||||||
| 173 | one | ||||||||||||||||||||||||||||
| 174 | one | ||||||||||||||||||||||||||||
| 175 | two | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ...which is what would happen if the state was reset after every render. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Remember than state is held for each document so... | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | use Template::Liquid; | ||||||||||||||||||||||||||||
| 182 | my $solution_a = Template::Liquid->parse(<<'TEMPLATE'); | ||||||||||||||||||||||||||||
| 183 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 184 | {% cycle 'group 2': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 185 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 186 | TEMPLATE | ||||||||||||||||||||||||||||
| 187 | my $solution_b = Template::Liquid->parse(<<'TEMPLATE'); | ||||||||||||||||||||||||||||
| 188 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 189 | {% cycle 'group 2': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 190 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 191 | TEMPLATE | ||||||||||||||||||||||||||||
| 192 | my $solution_c = Template::Liquid->parse(<<'TEMPLATE'); | ||||||||||||||||||||||||||||
| 193 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 194 | {% cycle 'group 2': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 195 | {% cycle 'group 1': 'one', 'two', 'three' %} | ||||||||||||||||||||||||||||
| 196 | TEMPLATE | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | print $solution_a->render(); | ||||||||||||||||||||||||||||
| 199 | print $solution_b->render(); | ||||||||||||||||||||||||||||
| 200 | print $solution_c->render(); | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | ...would print... | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | one | ||||||||||||||||||||||||||||
| 205 | one | ||||||||||||||||||||||||||||
| 206 | two | ||||||||||||||||||||||||||||
| 207 | one | ||||||||||||||||||||||||||||
| 208 | one | ||||||||||||||||||||||||||||
| 209 | two | ||||||||||||||||||||||||||||
| 210 | one | ||||||||||||||||||||||||||||
| 211 | one | ||||||||||||||||||||||||||||
| 212 | two | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 See Also | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 Author | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/ | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | The original Liquid template system was developed by jadedPixel | ||||||||||||||||||||||||||||
| 223 | (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/). | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 License and Legal | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 230 | the terms of The Artistic License 2.0. See the F<LICENSE> file included with | ||||||||||||||||||||||||||||
| 231 | this distribution or http://www.perlfoundation.org/artistic_license_2_0. For | ||||||||||||||||||||||||||||
| 232 | clarification, see http://www.perlfoundation.org/artistic_2_0_notes. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | When separated from the distribution, all original POD documentation is covered | ||||||||||||||||||||||||||||
| 235 | by the Creative Commons Attribution-Share Alike 3.0 License. See | ||||||||||||||||||||||||||||
| 236 | http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification, | ||||||||||||||||||||||||||||
| 237 | see http://creativecommons.org/licenses/by-sa/3.0/us/. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||||||||||