File Coverage

blib/lib/Template/Liquid/Tag/If.pm
Criterion Covered Total %
statement 32 32 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 7 7 100.0
pod 0 3 0.0
total 45 53 84.9


line stmt bran cond sub pod time code
1             package Template::Liquid::Tag::If;
2             our $VERSION = '1.0.23';
3 25     25   208 use strict;
  25         66  
  25         707  
4 25     25   140 use warnings;
  25         49  
  25         878  
5             require Template::Liquid::Error;
6             require Template::Liquid::Utility;
7 25     25   149 use base 'Template::Liquid::Tag';
  25         68  
  25         15340  
8 25     25   84 sub import { Template::Liquid::register_tag('if') }
9              
10             sub new {
11 97     97 0 225 my ($class, $args) = @_;
12             raise Template::Liquid::Error {type => 'Context',
13             template => $args->{template},
14             message => 'Missing template argument',
15             fatal => 1
16             }
17 97 50       257 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 97 50       223 if !defined $args->{'parent'};
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 97 50 33     591 if !defined $args->{'attrs'} || $args->{'attrs'} !~ m[\S$]o;
31 97         216 my $condition = $args->{'attrs'};
32             my $s = bless {name => $args->{'tag_name'} . '-' . $condition,
33             blocks => [],
34             tag_name => $args->{'tag_name'},
35             template => $args->{'template'},
36             parent => $args->{'parent'},
37             markup => $args->{'markup'},
38 97         1306 end_tag => 'end' . $args->{'tag_name'},
39             conditional_tag => qr[^(?:else|else?if)$]o
40             }, $class;
41 97         313 return $s;
42             }
43              
44             sub push_block {
45 37     37 0 155 my ($s, $args) = @_;
46             my $block
47             = Template::Liquid::Block->new({tag_name => $args->{'tag_name'},
48             attrs => $args->{'attrs'},
49 37         192 template => $args->{'template'},
50             parent => $s
51             }
52             );
53             { # finish previous block
54 37         95 ${$s->{'blocks'}[-1]}{'nodelist'} = $s->{'nodelist'};
  37         64  
  37         107  
55 37         80 $s->{'nodelist'} = [];
56             }
57 37         56 push @{$s->{'blocks'}}, $block;
  37         71  
58 37         89 return $block;
59             }
60              
61             sub render {
62 140     140 0 297 my ($s) = @_;
63 140         195 for my $block (@{$s->{'blocks'}}) {
  140         304  
64 180 100       264 return $block->render() if grep {$_} @{$block->{'conditions'}};
  176         1072  
  180         377  
65             }
66             }
67             1;
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =begin stopwords
74              
75             Lütke jadedPixel
76              
77             =end stopwords
78              
79             =head1 NAME
80              
81             Template::Liquid::Tag::If - Basic If/Elsif/Else Construct
82              
83             =head1 Description
84              
85             If I need to describe if/else to you... Oy. C<if> executes the statement once
86             if and I<only> if the condition is true. If the condition is false, the first
87             C<elseif> condition is evaluated. If that is also false it continues in the
88             same pattern until we find a true condition or a fallback C<else> tag.
89              
90             =head2 Compound Inequalities
91              
92             Liquid supports compound inequalities. Try these...
93              
94             {% if some.value == 3 and some.string contains 'find me' %}
95             Wow! It's a match...
96             {% elseif some.value == 4 or 3 < some.value %}
97             Wow! It's a... different... match...
98             {% endif %}
99              
100             =head1 Bugs
101              
102             Liquid's (and by extension L<Template::Liquid|Template::Liquid>'s) treatment of
103             compound inequalities is broken. For example...
104              
105             {% if 'This and that' contains 'that' and 1 == 3 %}
106              
107             ...would be parsed as if it were...
108              
109             if ( "'This" && ( "that'" =~ m[and] ) ) { ...
110              
111             ...but it I<should> look like...
112              
113             if ( ( 'This and that' =~ m[that]) && ( 1 == 3 ) ) { ...
114              
115             It's just... not pretty but I'll work on it. The actual problem is in
116             L<Template::Liquid::Block|Template::Liquid::Block> if you feel like lending a
117             hand. Wink, wink.
118              
119             =head1 See Also
120              
121             See L<Template::Liquid::Condition|Template::Liquid::Condition> for a list of
122             supported inequality types.
123              
124             =head1 Author
125              
126             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
127              
128             The original Liquid template system was developed by jadedPixel
129             (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
130              
131             =head1 License and Legal
132              
133             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
134              
135             This program is free software; you can redistribute it and/or modify it under
136             the terms of The Artistic License 2.0. See the F<LICENSE> file included with
137             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
138             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
139              
140             When separated from the distribution, all original POD documentation is covered
141             by the Creative Commons Attribution-Share Alike 3.0 License. See
142             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
143             see http://creativecommons.org/licenses/by-sa/3.0/us/.
144              
145             =cut