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