File Coverage

blib/lib/Template/Liquid/Variable.pm
Criterion Covered Total %
statement 35 36 97.2
branch 10 16 62.5
condition 1 3 33.3
subroutine 6 6 100.0
pod 0 2 0.0
total 52 63 82.5


line stmt bran cond sub pod time code
1             our $VERSION = '1.0.22';
2             use strict;
3 24     24   145 use warnings;
  24         43  
  24         569  
4 24     24   112 use base 'Template::Liquid::Document';
  24         55  
  24         722  
5 24     24   137 use Template::Liquid::Error;
  24         55  
  24         1961  
6 24     24   8571  
  24         53  
  24         8254  
7             my ($class, $args) = @_;
8             raise Template::Liquid::Error {type => 'Context',
9 293     293 0 545 template => $args->{template},
10             message => 'Missing template argument',
11             fatal => 1
12             }
13             if !defined $args->{'template'} ||
14             !$args->{'template'}->isa('Template::Liquid');
15             raise Template::Liquid::Error {type => 'Context',
16 293 50 33     1426 template => $args->{template},
17             message => 'Missing parent argument',
18             fatal => 1
19             }
20             if !defined $args->{'parent'};
21             raise Template::Liquid::Error {
22 293 50       523 template => $args->{template},
23             type => 'Syntax',
24             message => 'Missing variable name in ' . $args->{'markup'},
25             fatal => 1
26             }
27             if !defined $args->{'variable'};
28             return bless $args, $class;
29 293 50       487 }
30 293         791  
31             my ($s) = @_;
32             my $val = $s->{template}{context}->get($s->{variable});
33             { # XXX - Duplicated in Template::Liquid::Assign::render
34 534     534 0 838 if (scalar @{$s->{filters}}) {
35 534         1183 my %_filters = $s->{template}->filters;
36             FILTER: for my $filter (@{$s->{filters}}) {
37 534 100       736 my ($name, $args) = @$filter;
  534         598  
  534         1052  
38 158         369 my $package = $_filters{$name};
39 158         452 my $call = $package ? $package->can($name) : ();
  158         260  
40 168         287 if ($call) {
41 168         233  
42 168 50       665 #use Data::Dump qw[dump];
43 168 50       339 #warn sprintf 'Before %s(%s): %s', $name, dump($args), dump($val);
44             #warn $s->{template}{context}->get($args->[0]) if ref $args;
45             $val = $call->(
46             $val,
47             map { $s->{template}{context}->get($_); } @$args
48             );
49              
50 168         315 #warn sprintf 'After %s(%s): %s', $name, dump($args), dump($val);
  118         250  
51             next FILTER;
52             }
53             raise Template::Liquid::Error {type => 'UnknownFilter',
54 168         980 template => $s->{template},
55             message => $name,
56             fatal => 0
57             };
58 0         0 }
59             }
60             }
61              
62             #warn '---> ' . $s->{variable} . ' ===> ' .$val;
63             return join '', @$val if ref $val eq 'ARRAY';
64             return join '', keys %$val if ref $val eq 'HASH';
65             return $val;
66 534 100       994 }
67 533 50       815 1;
68 533         1046  
69             =pod
70              
71             =head1 NAME
72              
73             Template::Liquid::Variable - Generic Value Container
74              
75             =head1 Description
76              
77             This class can hold just about anything. This is the class responsible for
78             handling echo statements:
79              
80             Hello, {{ name }}. It's been {{ lastseen | date_relative }} since you
81             logged in.
82              
83             Internally, a variable is the basic container for everything; lists, scalars,
84             hashes, and even objects.
85              
86             L<Filters|Template::Liquid::Filters> are applied to variables during the render
87             stage.
88              
89             =head1 Author
90              
91             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
92              
93             CPAN ID: SANKO
94              
95             =head1 License and Legal
96              
97             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
98              
99             This program is free software; you can redistribute it and/or modify it under
100             the terms of L<The Artistic License
101             2.0|http://www.perlfoundation.org/artistic_license_2_0>. See the F<LICENSE>
102             file included with this distribution or L<notes on the Artistic License
103             2.0|http://www.perlfoundation.org/artistic_2_0_notes> for clarification.
104              
105             When separated from the distribution, all original POD documentation is covered
106             by the L<Creative Commons Attribution-Share Alike 3.0
107             License|http://creativecommons.org/licenses/by-sa/3.0/us/legalcode>. See the
108             L<clarification of the
109             CCA-SA3.0|http://creativecommons.org/licenses/by-sa/3.0/us/>.
110              
111             =cut