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.22';
2             use strict;
3 24     24   146 use warnings;
  24         47  
  24         638  
4 24     24   116 require Template::Liquid::Error;
  24         39  
  24         878  
5             require Template::Liquid::Utility;
6             BEGIN { use base 'Template::Liquid::Tag'; }
7 24     24   109  
  24     0   42  
  24         15410  
  0         0  
8 24     24   72 my ($class, $args) = @_;
9             raise Template::Liquid::Error {type => 'Context',
10             template => $args->{template},
11 63     63 0 124 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       145 message => 'Missing parent argument',
18             fatal => 1
19             }
20             if !defined $args->{'parent'};
21             raise Template::Liquid::Error {
22             type => 'Syntax',
23 63 50       122 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       209 $args->{'name'} = 'a-' . $args->{'attrs'};
31             $args->{'filters'} = [];
32 63         319 if ($filters) {
33 63         362 for my $filter (split $Template::Liquid::Utility::FilterSeparator,
34 63         124 $filters) {
35 63 100       145 my ($filter, $f_args)
36 27         103 = split $Template::Liquid::Utility::FilterArgumentSeparator,
37             $filter, 2;
38 30         111 $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         117 = !defined $f_args ? () : grep { defined $_ }
42 30         66 $f_args
43             =~ m[$Template::Liquid::Utility::VariableFilterArgumentParser]g;
44 30 100       166 push @{$args->{'filters'}}, [$filter, \@f_args];
  87         164  
45             }
46             }
47 30         49 return bless $args, $class;
  30         95  
48             }
49              
50 63         163 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 99 if (scalar @{$s->{filters}}) {
55 63         107 my %_filters = $s->{template}->filters;
56 63         180 FILTER: for my $filter (@{$s->{filters}}) {
57             my ($name, $args) = @$filter;
58 63 100       106 map { $_ = $s->{template}{context}->get($_) || $_ } @$args;
  63         84  
  63         163  
59 27         63 my $package = $_filters{$name};
60 27         89 my $call = $package ? $package->can($name) : ();
  27         51  
61 30         56 if ($call) {
62 30   33     48 $val = $call->($val, @$args);
  29         65  
63 30         59 next FILTER;
64 30 50       121 }
65 30 50       62 raise Template::Liquid::Error {
66 30         72 template => $s->{template},
67 30         180 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         209  
79 63         146 =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