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.5';
3 2     2   38930 use Carp qw[confess];
  2         2  
  2         118  
4 2     2   484 use Template::Liquid;
  2         18972  
  2         42  
5 2     2   17 use base 'Template::Liquid::Tag';
  2         3  
  2         449  
6 2     2   16 sub import { Template::Liquid::register_tag('dump') }
7              
8             sub new {
9 1     1 0 141 my ($class, $args, $tokens) = @_;
10 1 50       4 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         7 parent => $args->{'parent'},
17             }, $class;
18 1         3 return $s;
19             }
20              
21             sub render {
22 1     1 0 57 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       11 : $s->{template}{context}->get($var);
    50          
28 1 50       36 if (eval { require Data::Dump }) { # Better
  1         602  
29 1         3872 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              
53             =head1 Description
54              
55             This is a dead simple demonstration of
56             L.
57              
58             This tag attempts to use L and L to create
59             stringified versions of data structures...
60              
61             use Template::Liquid;
62             use Template::LiquidX::Tag::Dump;
63             warn Template::Liquid->parse("{%dump env%}")->render(env => \%ENV);
64              
65             ...or the entire current scope with C<.>...
66              
67             use Template::Liquid;
68             use Template::LiquidX::Tag::Include;
69             warn Template::Liquid->parse('{%dump .%}')
70             ->render(env => \%ENV, inc => \@INC);
71              
72             ...or the entire stack of scopes with C<.*>...
73              
74             use Template::Liquid;
75             use Template::LiquidX::Tag::Include;
76             warn Template::Liquid->parse('{%for x in (1..1) %}{%dump .*%}{%endfor%}')
77             ->render();
78              
79             =head1 Notes
80              
81             This is a 5m hack and is subject to change ...I've included no error handling
82             and it may be completly broken. For a better example, see
83             L.
84              
85             =head1 See Also
86              
87             Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
88              
89             L's section on
90             custom tags.
91              
92             =head1 Author
93              
94             Sanko Robinson - http://sankorobinson.com/
95              
96             =head1 License and Legal
97              
98             Copyright (C) 2009-2016 by Sanko Robinson Esanko@cpan.orgE
99              
100             This program is free software; you can redistribute it and/or modify it under
101             the terms of The Artistic License 2.0. See the F file included with
102             this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
103             clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
104              
105             When separated from the distribution, all original POD documentation is
106             covered by the Creative Commons Attribution-Share Alike 3.0 License. See
107             http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For
108             clarification, see http://creativecommons.org/licenses/by-sa/3.0/us/.
109              
110             =cut