File Coverage

blib/lib/Template/Liquid/Tag/Assign.pm
Criterion Covered Total %
statement 46 48 95.8
branch 11 16 68.7
condition 1 3 33.3
subroutine 6 7 85.7
pod 0 2 0.0
total 64 76 84.2


line stmt bran cond sub pod time code
1             our $VERSION = '1.0.21';
2             use strict;
3 24     24   150 use warnings;
  24         43  
  24         613  
4 24     24   100 require Template::Liquid::Error;
  24         36  
  24         890  
5             require Template::Liquid::Utility;
6             BEGIN { use base 'Template::Liquid::Tag'; }
7 24     24   101  
  24     0   44  
  24         14873  
  0         0  
8 24     24   68 my ($class, $args) = @_;
9             raise Template::Liquid::Error {type => 'Context',
10             template => $args->{template},
11 63     63 0 122 message => 'Missing template argument',
12             fatal => 1
13             }
14             if !defined $args->{'template'};
15             raise Template::Liquid::Error {type => 'Context',
16             template => $args->{template},
17 63 50       154 message => 'Missing parent argument',
18             fatal => 1
19             }
20             if !defined $args->{'parent'};
21             raise Template::Liquid::Error {
22             type => 'Syntax',
23 63 50       119 template => $args->{template},
24             message => 'Missing argument list in ' . $args->{'markup'},
25             fatal => 1
26             }
27             if !defined $args->{'attrs'};
28             ($args->{'variable'}, $args->{'value'}, my $filters)
29             = split m[\s*[=\|]\s+?]o, $args->{'attrs'}, 3;
30 63 50       117 $args->{'name'} = 'a-' . $args->{'attrs'};
31             $args->{'filters'} = [];
32 63         308 if ($filters) {
33 63         333 for my $filter (split $Template::Liquid::Utility::FilterSeparator,
34 63         128 $filters) {
35 63 100       117 my ($filter, $f_args)
36 27         109 = split $Template::Liquid::Utility::FilterArgumentSeparator,
37             $filter, 2;
38 30         114 $filter =~ s[\s*$][]o; # XXX - the splitter should clean...
39             $filter =~ s[^\s*][]o; # XXX - ...this up for us.
40             my @f_args
41 30         116 = !defined $f_args ? () : grep { defined $_ }
42 30         69 $f_args
43             =~ m[$Template::Liquid::Utility::VariableFilterArgumentParser]g;
44 30 100       180 push @{$args->{'filters'}}, [$filter, \@f_args];
  87         173  
45             }
46             }
47 30         43 return bless $args, $class;
  30         95  
48             }
49              
50 63         175 my $s = shift;
51             my $var = $s->{'variable'};
52             my $val = $s->{template}{context}->get($s->{'value'});
53             { # XXX - Duplicated in Template::Liquid::Variable::render
54 63     63 0 97 if (scalar @{$s->{filters}}) {
55 63         109 my %_filters = $s->{template}->filters;
56 63         184 FILTER: for my $filter (@{$s->{filters}}) {
57             my ($name, $args) = @$filter;
58 63 100       110 map { $_ = $s->{template}{context}->get($_) || $_ } @$args;
  63         108  
  63         151  
59 27         79 my $package = $_filters{$name};
60 27         90 my $call = $package ? $package->can($name) : ();
  27         60  
61 30         57 if ($call) {
62 30   33     49 $val = $call->($val, @$args);
  29         71  
63 30         69 next FILTER;
64 30 50       129 }
65 30 50       60 raise Template::Liquid::Error {
66 30         78 template => $s->{template},
67 30         167 type => 'Filter',
68             message => "Filter '$name' not found",
69             fatal => 1
70             };
71 0         0 }
72             }
73             }
74             $s->{template}{context}->set($var, $val);
75             return '';
76             }
77             1;
78 63         212  
79 63         205 =pod
80              
81             =encoding UTF-8
82              
83             =begin stopwords
84              
85             Lütke jadedPixel
86              
87             =end stopwords
88              
89             =head1 NAME
90              
91             Template::Liquid::Tag::Assign - Variable assignment construct
92              
93             =head1 Synopsis
94              
95             {% assign some.variable = 'this value' %}
96              
97             =head1 Description
98              
99             You can store data in your own variables for later use as output or in other
100             tags. The simplest way to create a variable is with the C<assign> tag which a
101             rather straightforward syntax.
102              
103             {% assign person.name = 'john' %}
104             Hello, {{ person.name | capitalize }}.
105              
106             You can modify the value C<before> assignment with
107             L<filters|Template::Liquid::Filters>.
108              
109             {% assign person.name = 'john' | capitalize %}
110             Hello, {{ person.name }}.
111              
112             =head1 See Also
113              
114             Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
115              
116             L<Template::Liquid|Template::Liquid/"Create your own filters">'s docs on custom
117             filter creation
118              
119             =head1 Author
120              
121             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
122              
123             The original Liquid template system was developed by jadedPixel
124             (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
125              
126             =head1 License and Legal
127              
128             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
129              
130             This program is free software; you can redistribute it and/or modify it under
131             the terms of The Artistic License 2.0. See the F<LICENSE> file included with
132             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
133             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
134              
135             When separated from the distribution, all original POD documentation is covered
136             by the Creative Commons Attribution-Share Alike 3.0 License. See
137             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
138             see http://creativecommons.org/licenses/by-sa/3.0/us/.
139              
140             =cut