| 1 | package Template::Liquid::Tag::Assign; | ||||||||||||||||||||
| 2 | 25 | our $VERSION = '1.0.23'; | |||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | require Template::Liquid::Error; | |||||||||||||||||||
| 6 | 25 | require Template::Liquid::Utility; | |||||||||||||||||||
| 7 | 25 | ▶ | BEGIN { use base 'Template::Liquid::Tag'; } | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 25 | ▶ | sub import { Template::Liquid::register_tag('assign') } | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 65 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||
| 11 | 65 | 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 | ▶ | if !defined $args->{'template'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | raise Template::Liquid::Error {type => 'Context', | ||||||||||||||||||||
| 19 | template => $args->{template}, | ||||||||||||||||||||
| 20 | message => 'Missing parent argument', | ||||||||||||||||||||
| 21 | fatal => 1 | ||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | 65 | ▶ | if !defined $args->{'parent'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | if !defined $args->{'attrs'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | ($args->{'variable'}, $args->{'value'}, my $filters) | ||||||||||||||||||||
| 32 | 65 | = split m[\s*[=\|]\s+?]o, $args->{'attrs'}, 3; | |||||||||||||||||||
| 33 | 65 | $args->{'name'} = 'a-' . $args->{'attrs'}; | |||||||||||||||||||
| 34 | 65 | $args->{'filters'} = []; | |||||||||||||||||||
| 35 | 65 | ▶ | if ($filters) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 28 | for my $filter (split $Template::Liquid::Utility::FilterSeparator, | |||||||||||||||||||
| 37 | $filters) { | ||||||||||||||||||||
| 38 | 31 | my ($filter, $f_args) | |||||||||||||||||||
| 39 | = split $Template::Liquid::Utility::FilterArgumentSeparator, | ||||||||||||||||||||
| 40 | $filter, 2; | ||||||||||||||||||||
| 41 | 31 | $filter =~ s[\s*$][]o; # XXX - the splitter should clean... | |||||||||||||||||||
| 42 | 31 | $filter =~ s[^\s*][]o; # XXX - ...this up for us. | |||||||||||||||||||
| 43 | my @f_args | ||||||||||||||||||||
| 44 | 31 | ▶ | = !defined $f_args ? () : grep { defined $_ } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | $f_args | ||||||||||||||||||||
| 46 | =~ m[$Template::Liquid::Utility::VariableFilterArgumentParser]g; | ||||||||||||||||||||
| 47 | 31 | push @{$args->{'filters'}}, [$filter, \@f_args]; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 65 | return bless $args, $class; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 65 | ▶ | sub render { | ||||||||||||||||||
|
Subroutine
render: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||
| 54 | 65 | my $s = shift; | |||||||||||||||||||
| 55 | 65 | my $var = $s->{'variable'}; | |||||||||||||||||||
| 56 | 65 | my $val = $s->{template}{context}->get($s->{'value'}); | |||||||||||||||||||
| 57 | { # XXX - Duplicated in Template::Liquid::Variable::render | ||||||||||||||||||||
| 58 | 65 | ▶ | if (scalar @{$s->{filters}}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 28 | my %_filters = $s->{template}->filters; | |||||||||||||||||||
| 60 | 28 | FILTER: for my $filter (@{$s->{filters}}) { | |||||||||||||||||||
| 61 | 31 | my ($name, $args) = @$filter; | |||||||||||||||||||
| 62 | 31 | ▶ | map { $_ = $s->{template}{context}->get($_) || $_ } @$args; | ||||||||||||||||||
|
Condition33%1/3
$s->{'template'}{'context'}->get($_) || $_
MC/DC0%0/2
$s->{'template'}{'context'}->get($_) || $_
$s->{'template'}{'context'}->get($_)
$_
Truth table33%1/3
A || B
A= $s->{'template'}{'context'}->get($_)B= $_
| |||||||||||||||||||||
| 63 | 31 | my $package = $_filters{$name}; | |||||||||||||||||||
| 64 | 31 | ▶ | my $call = $package ? $package->can($name) : (); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 31 | ▶ | if ($call) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 31 | $val = $call->($val, @$args); | |||||||||||||||||||
| 67 | 31 | next FILTER; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | raise Template::Liquid::Error { | ||||||||||||||||||||
| 70 | template => $s->{template}, | ||||||||||||||||||||
| 71 | 0 | type => 'Filter', | |||||||||||||||||||
| 72 | message => "Filter '$name' not found", | ||||||||||||||||||||
| 73 | fatal => 1 | ||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 65 | $s->{template}{context}->set($var, $val); | |||||||||||||||||||
| 79 | 65 | return ''; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 25 | 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 | ||||||||||||||||||||