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             package Template::Liquid::Block;
2             our $VERSION = '1.0.23';
3             require Template::Liquid::Error;
4 25     25   176 use base 'Template::Liquid::Document';
  25         54  
  25         1906  
5 25     25   168 use strict;
  25         67  
  25         480  
6 25     25   154 use warnings;
  25         51  
  25         13295  
7              
8             sub new {
9 276     276 0 621 my ($class, $args) = @_;
10             raise Template::Liquid::Error {type => 'Context',
11             template => $args->{template},
12             message => 'Missing template argument',
13             fatal => 1
14             }
15 276 50       625 if !defined $args->{'template'};
16             raise Template::Liquid::Error {type => 'Context',
17             template => $args->{template},
18             message => 'Missing parent argument',
19             fatal => 1
20             }
21 276 50       651 if !defined $args->{'parent'};
22             raise Template::Liquid::Error {
23             template => $args->{template},
24             type => 'Syntax',
25             message => 'else tags are non-conditional: ' . $args->{'markup'},
26             fatal => 1
27             }
28 276 50 66     732 if $args->{'tag_name'} eq 'else' && $args->{'attrs'};
29             my $s = bless {tag_name => 'b-' . $args->{'tag_name'},
30             conditions => undef,
31             nodelist => [],
32             template => $args->{'template'},
33 276         1244 parent => $args->{'parent'},
34             }, $class;
35             $s->{'conditions'} = (
36             $args->{'tag_name'} eq 'else' ? [1] : sub { # Oh, what a mess...
37             my @conditions
38             = split
39             m/(?:\s+\b(and|or)\b\s+)(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)/o,
40             $args->{parent}->{tag_name} eq 'for'
41             ? 1
42 252 50   252   1492 : (defined $args->{'attrs'} ? $args->{'attrs'} : '');
    100          
43 252         449 my @equality;
44 252         678 while (my $x = shift @conditions) {
45             push @equality,
46             (
47             $x
48             =~ m/(?:\b(and|or)\b)(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)/o
49             ? bless({template => $args->{'template'},
50             parent => $s,
51             condition => $x,
52             lvalue => pop @equality,
53             rvalue =>
54             Template::Liquid::Condition->new(
55             {template => $args->{'template'},
56             parent => $s,
57             attrs => shift @conditions
58             }
59             )
60             },
61             'Template::Liquid::Condition'
62             )
63             : Template::Liquid::Condition->new(
64             {attrs => $x,
65 275 100       1916 template => $args->{'template'},
66             parent => $s,
67             }
68             )
69             );
70             }
71 252         701 \@equality;
72             }
73 276 100       1728 ->()
74             );
75 276         1722 return $s;
76             }
77             1;
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =begin stopwords
84              
85             sorta whitespace Lütke jadedPixel
86              
87             =end stopwords
88              
89             =head1 NAME
90              
91             Template::Liquid::Block - Simple Node Type
92              
93             =head1 Description
94              
95             There's not really a lot to say about basic blocks. The real action is in the
96             classes which make use of them or subclass it. See
97             L<if|Template::Liquid::Tag::If>, L<unless|Template::Liquid::Tag::Unless>, or
98             L<case|Template::Liquid::Tag::Case>.
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 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.
116              
117             =head1 See Also
118              
119             See L<Template::Liquid::Condition|Template::Liquid::Condition> for a list of
120             supported inequalities.
121              
122             =head1 Author
123              
124             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
125              
126             The original Liquid template system was developed by jadedPixel
127             (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
128              
129             =head1 License and Legal
130              
131             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
132              
133             This program is free software; you can redistribute it and/or modify it under
134             the terms of The Artistic License 2.0. See the F<LICENSE> file included with
135             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
136             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
137              
138             When separated from the distribution, all original POD documentation is covered
139             by the Creative Commons Attribution-Share Alike 3.0 License. See
140             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
141             see http://creativecommons.org/licenses/by-sa/3.0/us/.
142              
143             =cut