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