File Coverage

blib/lib/Template/LiquidX/Tag/Dump.pm
Criterion Covered Total %
statement 21 24 87.5
branch 4 8 50.0
condition 1 2 50.0
subroutine 6 6 100.0
pod 0 2 0.0
total 32 42 76.1


line stmt bran cond sub pod time code
1             package Template::LiquidX::Tag::Dump;
2             our $VERSION = '1.0.6';
3 2     2   35261 use Carp qw[confess];
  2         2  
  2         118  
4 2     2   494 use Template::Liquid;
  2         19531  
  2         43  
5 2     2   19 use base 'Template::Liquid::Tag';
  2         2  
  2         448  
6 2     2   15 sub import { Template::Liquid::register_tag('dump') }
7              
8             sub new {
9 1     1 0 91 my ($class, $args, $tokens) = @_;
10 1 50       3 confess 'Missing template' if !defined $args->{'template'};
11 1   50     3 $args->{'attrs'} ||= '.';
12             my $s = bless {name => 'dump-' . $args->{'attrs'},
13             tag_name => $args->{'tag_name'},
14             variable => $args->{'attrs'},
15             template => $args->{'template'},
16 1         5 parent => $args->{'parent'},
17             }, $class;
18 1         2 return $s;
19             }
20              
21             sub render {
22 1     1 0 45 my $s = shift;
23 1         1 my $var = $s->{'variable'};
24             $var
25             = $var eq '.' ? $s->{template}{context}{scopes}
26             : $var eq '.*' ? [$s->{template}{context}{scopes}]
27 1 50       6 : $s->{template}{context}->get($var);
    50          
28 1 50       32 if (eval { require Data::Dump }) { # Better
  1         485  
29 1         3572 return Data::Dump::pp($var);
30             }
31             else { # CORE
32 0           require Data::Dumper;
33 0           return Data::Dumper::Dumper($var);
34             }
35 0           return '';
36             }
37             1;
38              
39             =pod
40              
41             =encoding utf-8
42              
43             =head1 NAME
44              
45             Template::LiquidX::Tag::Dump - Simple Perl Structure Dumping Tag (Functioning Custom Tag Example)
46              
47             =head1 Synopsis
48              
49             use Template::Liquid;
50             use Template::LiquidX::Tag::Dump;
51             print Template::Liquid->parse("{%dump var%}")->render(var => [qw[some sort of data here]]);
52             # With Data::Dump installed: ["some", "sort", "of", "data", "here"]
53              
54             =head1 Description
55              
56             This is a dead simple demonstration of
57             L.
58              
59             This tag attempts to use L and L to create
60             stringified versions of data structures...
61              
62             use Template::Liquid;
63             use Template::LiquidX::Tag::Dump;
64             warn Template::Liquid->parse("{%dump env%}")->render(env => \%ENV);
65              
66             ...or the entire current scope with C<.>...
67              
68             use Template::Liquid;
69             use Template::LiquidX::Tag::Dump;
70             warn Template::Liquid->parse('{%dump .%}')
71             ->render(env => \%ENV, inc => \@INC);
72              
73             ...or the entire stack of scopes with C<.*>...
74              
75             use Template::Liquid;
76             use Template::LiquidX::Tag::Dump;
77             warn Template::Liquid->parse('{%for x in (1..1) %}{%dump .*%}{%endfor%}')
78             ->render();
79            
80             ...becomes (w/ Data::Dump installed)...
81              
82             do {
83             my $a = [
84             [
85             {
86             forloop => {
87             first => 1,
88             index => 1,
89             index0 => 0,
90             last => 1,
91             length => 1,
92             limit => 1,
93             name => "x-(1..1)",
94             offset => 0,
95             rindex => 1,
96             rindex0 => 0,
97             sorted => undef,
98             type => "ARRAY",
99             },
100             x => 1,
101             },
102             'fix',
103             ],
104             ];
105             $a->[0][1] = $a->[0][0];
106             $a;
107             }
108              
109             Notice even the internal C variable is included in the dump.
110              
111             =head1 Notes
112              
113             This is a 5m hack and is subject to change ...I've included no error handling
114             and it may be completly broken. For a better example, see
115             L.
116              
117             =head1 See Also
118              
119             Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
120              
121             L's section on
122             custom tags.
123              
124             =head1 Author
125              
126             Sanko Robinson - http://sankorobinson.com/
127              
128             =head1 License and Legal
129              
130             Copyright (C) 2009-2016 by Sanko Robinson Esanko@cpan.orgE
131              
132             This program is free software; you can redistribute it and/or modify it under
133             the terms of The Artistic License 2.0. See the F file included with
134             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
135             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
136              
137             When separated from the distribution, all original POD documentation is
138             covered by the Creative Commons Attribution-Share Alike 3.0 License. See
139             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For
140             clarification, see http://creativecommons.org/licenses/by-sa/3.0/us/.
141              
142             =cut