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