File Coverage

blib/lib/Template/Liquid/Tag/Capture.pm
Criterion Covered Total %
statement 26 28 92.8
branch 7 12 58.3
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 39 49 79.5


line stmt bran cond sub pod time code
1             package Template::Liquid::Tag::Capture;
2             our $VERSION = '1.0.23';
3 25     25   183 use strict;
  25         52  
  25         764  
4 25     25   132 use warnings;
  25         74  
  25         1158  
5             require Template::Liquid::Error;
6             require Template::Liquid::Utility;
7 25     25   190 BEGIN { use base 'Template::Liquid::Tag'; }
  25     0   56  
  25         12556  
  0         0  
8 25     25   98 sub import { Template::Liquid::register_tag('capture') }
9              
10             sub new {
11 11     11 0 27 my ($class, $args) = @_;
12             raise Template::Liquid::Error {type => 'Context',
13             template => $args->{template},
14             message => 'Missing template argument',
15             fatal => 1
16             }
17 11 50       30 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 11 50       28 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 11 50       27 if !defined $args->{'attrs'};
31 11 50       78 if ($args->{'attrs'} !~ qr[^(\S+)\s*?$]o) {
32             raise Template::Liquid::Error {
33             type => 'Syntax',
34             template => $args->{template},
35 0         0 message => 'Bad argument list in ' . $args->{'markup'},
36             fatal => 1
37             };
38             }
39             my $s = bless {name => 'c-' . $1,
40             nodelist => [],
41             tag_name => $args->{'tag_name'},
42             variable_name => $1,
43             end_tag => 'end' . $args->{'tag_name'},
44             template => $args->{'template'},
45             parent => $args->{'parent'},
46 11         202 markup => $args->{'markup'},
47             }, $class;
48 11         35 return $s;
49             }
50              
51             sub render {
52 13     13 0 24 my ($s) = @_;
53 13         26 my $var = $s->{'variable_name'};
54 13         23 my $val = '';
55 13         19 for my $node (@{$s->{'nodelist'}}) {
  13         28  
56 29 100       70 my $rendering = ref $node ? $node->render() : $node;
57 29 50       87 $val .= defined $rendering ? $rendering : '';
58             }
59 13         48 $s->{template}{context}->set($var, $val);
60 13         32 return '';
61             }
62             1;
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =begin stopwords
69              
70             Lütke jadedPixel
71              
72             =end stopwords
73              
74             =head1 NAME
75              
76             Template::Liquid::Tag::Capture - Extended variable assignment construct
77              
78             =head1 Synopsis
79              
80             {% capture triple_x %}
81             {% for x in (1..3) %}{{ x }}{% endfor %}
82             {% endcapture %}
83              
84             =head1 Description
85              
86             If you want to combine a number of strings into a single string and save it to
87             a variable, you can do that with the C<capture> tag. This tag is a block which
88             "captures" whatever is rendered inside it, then assigns the captured value to
89             the given variable instead of rendering it to the screen.
90              
91             =head1 See Also
92              
93             The L<assign|Template::Liquid::Tag::Assign> tag.
94              
95             Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
96              
97             L<Template::Liquid|Template::Liquid/"Create your own filters">'s docs on custom
98             filter creation
99              
100             =head1 Author
101              
102             Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
103              
104             The original Liquid template system was developed by jadedPixel
105             (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
106              
107             =head1 License and Legal
108              
109             Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
110              
111             This program is free software; you can redistribute it and/or modify it under
112             the terms of The Artistic License 2.0. See the F<LICENSE> file included with
113             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
114             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
115              
116             When separated from the distribution, all original POD documentation is covered
117             by the Creative Commons Attribution-Share Alike 3.0 License. See
118             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification,
119             see http://creativecommons.org/licenses/by-sa/3.0/us/.
120              
121             =cut