blib/lib/Template/Liquid/Tag/Case.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 60.0% 12 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 80.8% 59 / 73 CC 12 SCAR 17.6
CC
12
Cov
84%
push_block
8.2
new
5.8
BEGIN
1.0
CRAP 5.8
« blib/lib/Template/Liquid/Tag/Cycle.pm ↑ Summary blib/lib/Template/Liquid/Tag/Raw.pm »
1 package Template::Liquid::Tag::Case;
2 24 our $VERSION = '1.0.22';
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use base 'Template::Liquid::Tag::If';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 require Template::Liquid::Error;
7 24 require Template::Liquid::Utility;
8 24 ▶sub import { Template::Liquid::register_tag('case') }
Subroutine import: called CC=1 SCAR=0.0
9  
10 sub new {
11 11 ▶ my ($class, $args) = @_;
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
12 raise Template::Liquid::Error {type => 'Context',
13 template => $args->{template},
14 message => 'Missing template argument',
15 fatal => 1
16 }
17 11 ▶ if !defined $args->{'template'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"template"} 0 11 11
18 raise Template::Liquid::Error {type => 'Context',
19 template => $args->{template},
20 message => 'Missing parent argument',
21 fatal => 1
22 }
23 11 ▶ if !defined $args->{'parent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"parent"} 0 11 11
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 11 ▶ if !defined $args->{'attrs'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"attrs"} 0 11 11
31 11 ▶ if ($args->{'attrs'} !~ m[\S$]o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"attrs"} =~ /\S$/o) 0 11 11
32 raise Template::Liquid::Error {
33 type => 'Syntax',
34 template => $args->{template},
35 0 message => 'Bad argument list in ' . $args->{'markup'},
36 fatal => 1
37 };
38 }
39 my $s = bless {name => $args->{'tag_name'} . '-' . $args->{'attrs'},
40 blocks => [],
41 tag_name => $args->{'tag_name'},
42 template => $args->{'template'},
43 parent => $args->{'parent'},
44 markup => $args->{'markup'},
45 value => $args->{'attrs'},
46 first_block => 0,
47 11 end_tag => 'end' . $args->{'tag_name'},
48 conditional_tag => qr[^(?:else|when)$]o
49 }, $class;
50 11 return $s;
51 }
52  
53 sub push_block {
54 26 ▶ my ($s, $args) = @_;
Subroutine push_block: called CC=8 SCAR=21.1
Pod undocumented
55 raise Template::Liquid::Error {type => 'Context',
56 template => $s->{template},
57 message => 'Missing template argument',
58 fatal => 1
59 }
60 26 ▶ if !defined $args->{'template'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"template"} 0 26 26
61 raise Template::Liquid::Error {type => 'Context',
62 template => $s->{template},
63 message => 'Missing parent argument',
64 fatal => 1
65 }
66 26 ▶ if !defined $args->{'parent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"parent"} 0 26 26
67 raise Template::Liquid::Error {
68 type => 'Syntax',
69 template => $s->{template},
70 message => 'Missing argument list in ' . $args->{'markup'},
71 fatal => 1
72 }
73 26 ▶ if !defined $args->{'attrs'} && $args->{'tag_name'} eq 'when';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args->{"attrs"} and $args->{"tag_name"} eq "when" 0 26 26
Condition66%2/3
not defined $args->{"attrs"} and $args->{"tag_name"} eq "when"
!ll&&!rl&&r
2330
MC/DC0%0/2
not defined $args->{"attrs"} and $args->{"tag_name"} eq "when"
not defined $args->{"attrs"} $args->{"tag_name"} eq "when"
Truth table66%2/3
not(A) and B
A= not defined $args->{"attrs"}B= $args->{"tag_name"} eq "when"
ABresult
0X0
100
111
74 26 ▶ if ($args->{'tag_name'} eq 'when') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"tag_name"} eq "when") 23 3 26
75 $args->{'attrs'} = join ' or ',
76 32 map { sprintf '%s == %s', $args->{'parent'}{'value'}, $_ }
77 32 grep { defined $_ }
78 23 $args->{'attrs'} =~ m[(${Template::Liquid::Utility::Expression})
79 (?:\s+or\s+|\s*\,\s*)?]oxmg;
80 }
81 my $block
82 = Template::Liquid::Block->new(
83 {tag_name => $args->{'tag_name'},
84 end_tag => 'end' . $args->{'tag_name'},
85 attrs => $args->{'attrs'},
86 26 template => $args->{'template'},
87 parent => $s
88 }
89 );
90  
91 # finish previous block if it exists
92 26 ${$s->{'blocks'}[-1]}{'nodelist'} = $s->{'nodelist'}
93 26 ▶ if scalar @{$s->{'blocks'}};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$s->{"blocks"};} 26 0 26
94 26 $s->{'nodelist'} = []; # Unline {%if%}, we *always* empty the
95  
96 # nodelist. This way, we ignore nodes that come before the first
97 # when/else block just like Liquid
98 26 push @{$s->{'blocks'}}, $block;
99 11 shift @{$s->{'blocks'}} # S::D->parse() pushes a dead first block
100 26 ▶ if $s->{'first_block'}++ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"first_block"}++ == 0 11 15 26
101 26 return $block;
102 }
103 24 1;
104  
105 =pod
106  
107 =encoding UTF-8
108  
109 =begin stopwords
110  
111 Lütke jadedPixel
112  
113 =end stopwords
114  
115 =head1 NAME
116  
117 Template::Liquid::Tag::Case - Switch Statement Construct
118  
119 =head1 Description
120  
121 If you need more conditions, you can use the C<case> tag. Note that, stuff that
122 comes before the first C<when> or C<else> is ignored. ...just as it is in
123 Liquid.
124  
125 =head1 Synopsis
126  
127 {% case condition %}
128 {% when 1 %}
129 hit 1
130 {% when 2 or 3 %}
131 hit 2 or 3
132 {% else %}
133 ... else ...
134 {% endcase %}
135  
136 ...or even...
137  
138 {% case template %}
139  
140 {% when 'label' %}
141 // {{ label.title }}
142 {% when 'product' %}
143 // {{ product.vendor | link_to_vendor }} / {{ product.title }}
144 {% else %}
145 // {{page_title}
146 {% endcase %}
147  
148 =head1 Author
149  
150 Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
151  
152 The original Liquid template system was developed by jadedPixel
153 (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
154  
155 =head1 License and Legal
156  
157 Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
158  
159 This program is free software; you can redistribute it and/or modify it under
160 the terms of The Artistic License 2.0. See the F<LICENSE> file included with
161 this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
162 clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
163  
164 When separated from the distribution, all original POD documentation is covered
165 by the Creative Commons Attribution-Share Alike 3.0 License. See
166 http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
167 see http://creativecommons.org/licenses/by-sa/3.0/us/.
168  
169 =cut
« blib/lib/Template/Liquid/Tag/Cycle.pm ↑ Summary blib/lib/Template/Liquid/Tag/Raw.pm »