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.21';
2             use strict;
3 24     24   132 use warnings;
  24         36  
  24         536  
4 24     24   120 use base 'Template::Liquid::Document';
  24         59  
  24         638  
5 24     24   129 use Template::Liquid::Error;
  24         63  
  24         1817  
6 24     24   8323  
  24         53  
  24         7992  
7             my ($class, $args) = @_;
8             raise Template::Liquid::Error {type => 'Context',
9 293     293 0 523 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     1380 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       558 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       467 }
30 293         755  
31             my ($s) = @_;
32             my $val = $s->{template}{context}->get($s->{variable});
33             { # XXX - Duplicated in Template::Liquid::Assign::render
34 534     534 0 815 if (scalar @{$s->{filters}}) {
35 534         1179 my %_filters = $s->{template}->filters;
36             FILTER: for my $filter (@{$s->{filters}}) {
37 534 100       785 my ($name, $args) = @$filter;
  534         585  
  534         1058  
38 158         328 my $package = $_filters{$name};
39 158         476 my $call = $package ? $package->can($name) : ();
  158         284  
40 168         302 if ($call) {
41 168         231  
42 168 50       730 #use Data::Dump qw[dump];
43 168 50       338 #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         399 #warn sprintf 'After %s(%s): %s', $name, dump($args), dump($val);
  118         269  
51             next FILTER;
52             }
53             raise Template::Liquid::Error {type => 'UnknownFilter',
54 168         1041 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       1013 }
67 533 50       803 1;
68 533         1019  
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