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