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 = v1.0.5;
3 2     2   34076 use Carp qw[confess];
  2         2  
  2         126  
4 2     2   452 use Template::Liquid;
  2         18361  
  2         40  
5 2     2   14 use base 'Template::Liquid::Tag';
  2         2  
  2         437  
6 2     2   13 sub import { Template::Liquid::register_tag('dump') }
7              
8             sub new {
9 1     1 0 94 my ($class, $args, $tokens) = @_;
10 1 50       3 confess 'Missing template' if !defined $args->{'template'};
11 1   50     2 $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         4 parent => $args->{'parent'},
17             }, $class;
18 1         2 return $s;
19             }
20              
21             sub render {
22 1     1 0 43 my $s = shift;
23 1         2 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       31 if (eval { require Data::Dump }) { # Better
  1         445  
29 1         3596 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