File Coverage

blib/lib/Template/Liquid/Block.pm
Criterion Covered Total %
statement 21 21 100.0
branch 10 14 71.4
condition 2 3 66.6
subroutine 5 5 100.0
pod 0 1 0.0
total 38 44 86.3


line stmt bran cond sub pod time code
1             our $VERSION = '1.0.22';
2             require Template::Liquid::Error;
3             use base 'Template::Liquid::Document';
4 24     24   133 use strict;
  24         41  
  24         1440  
5 24     24   128 use warnings;
  24         42  
  24         383  
6 24     24   106  
  24         48  
  24         10046  
7             my ($class, $args) = @_;
8             raise Template::Liquid::Error {type => 'Context',
9 264     264 0 442 template => $args->{template},
10             message => 'Missing template argument',
11             fatal => 1
12             }
13             if !defined $args->{'template'};
14             raise Template::Liquid::Error {type => 'Context',
15 264 50       526 template => $args->{template},
16             message => 'Missing parent argument',
17             fatal => 1
18             }
19             if !defined $args->{'parent'};
20             raise Template::Liquid::Error {
21 264 50       460 template => $args->{template},
22             type => 'Syntax',
23             message => 'else tags are non-conditional: ' . $args->{'markup'},
24             fatal => 1
25             }
26             if $args->{'tag_name'} eq 'else' && $args->{'attrs'};
27             my $s = bless {tag_name => 'b-' . $args->{'tag_name'},
28 264 50 66     578 conditions => undef,
29             nodelist => [],
30             template => $args->{'template'},
31             parent => $args->{'parent'},
32             }, $class;
33 264         1062 $s->{'conditions'} = (
34             $args->{'tag_name'} eq 'else' ? [1] : sub { # Oh, what a mess...
35             my @conditions
36             = split
37             m/(?:\s+\b(and|or)\b\s+)(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)/o,
38             $args->{parent}->{tag_name} eq 'for'
39             ? 1
40             : (defined $args->{'attrs'} ? $args->{'attrs'} : '');
41             my @equality;
42 242 50   242   1094 while (my $x = shift @conditions) {
    100          
43 242         362 push @equality,
44 242         583 (
45             $x
46             =~ m/(?:\b(and|or)\b)(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)/o
47             ? bless({template => $args->{'template'},
48             parent => $s,
49             condition => $x,
50             lvalue => pop @equality,
51             rvalue =>
52             Template::Liquid::Condition->new(
53             {template => $args->{'template'},
54             parent => $s,
55             attrs => shift @conditions
56             }
57             )
58             },
59             'Template::Liquid::Condition'
60             )
61             : Template::Liquid::Condition->new(
62             {attrs => $x,
63             template => $args->{'template'},
64             parent => $s,
65 265 100       1447 }
66             )
67             );
68             }
69             \@equality;
70             }
71 242         581 ->()
72             );
73 264 100       1295 return $s;
74             }
75 264         1318 1;
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =begin stopwords
82              
83             sorta whitespace Lütke jadedPixel
84              
85             =end stopwords
86              
87             =head1 NAME
88              
89             Template::Liquid::Block - Simple Node Type
90              
91             =head1 Description
92              
93             There's not really a lot to say about basic blocks. The real action is in the
94             classes which make use of them or subclass it. See
95             L<if|Template::Liquid::Tag::If>, L<unless|Template::Liquid::Tag::Unless>, or
96             L<case|Template::Liquid::Tag::Case>.
97              
98             =head1 Bugs
99              
100             Liquid's (and by extension L<Template::Liquid|Template::Liquid>'s) treatment of
101             compound inequalities is broken. For example...
102              
103             {% if 'This and that' contains 'that' and 1 == 3 %}
104              
105             ...would be parsed as if it were...
106              
107             if ( "'This" && ( "that'" =~ m[and] ) ) { ...
108              
109             ...but it should look like...
110              
111             if ( ( 'This and that' =~ m[that]) && ( 1 == 3 ) ) { ...
112              
113             It's just... not pretty but I'll work on it.
114              
115             =head1 See Also
116              
117             See L<Template::Liquid::Condition|Template::Liquid::Condition> for a list of
118             supported inequalities.
119              
120             =head1 Author
121              
122             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
123              
124             The original Liquid template system was developed by jadedPixel
125             (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
126              
127             =head1 License and Legal
128              
129             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
130              
131             This program is free software; you can redistribute it and/or modify it under
132             the terms of The Artistic License 2.0. See the F<LICENSE> file included with
133             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
134             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
135              
136             When separated from the distribution, all original POD documentation is covered
137             by the Creative Commons Attribution-Share Alike 3.0 License. See
138             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
139             see http://creativecommons.org/licenses/by-sa/3.0/us/.
140              
141             =cut