| 1 | package Template::LiquidX::Tag::Dump; | ||||||||||||||
| 2 | 2 | our $VERSION = '1.0.5'; | |||||||||||||
| 3 | 2 | ▶ | use Carp qw[confess]; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use Template::Liquid; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use base 'Template::Liquid::Tag'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | sub import { Template::Liquid::register_tag('dump') } | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | sub new { | ||||||||||||||
| 9 | 1 | ▶ | my ($class, $args, $tokens) = @_; | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||
| 10 | 1 | ▶ | confess 'Missing template' if !defined $args->{'template'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 11 | 1 | ▶ | $args->{'attrs'} ||= '.'; | ||||||||||||
|
Condition50%1/2
$args->{"attrs"} ||= "."
MC/DC0%0/1
$args->{"attrs"} ||= "."
$args->{"attrs"}
Truth table50%1/2
A
A= $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 | parent => $args->{'parent'}, | |||||||||||||
| 17 | }, $class; | ||||||||||||||
| 18 | 1 | return $s; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | sub render { | ||||||||||||||
| 22 | 1 | ▶ | my $s = shift; | ||||||||||||
|
Subroutine
render: called CC=4 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 23 | 1 | my $var = $s->{'variable'}; | |||||||||||||
| 24 | $var | ||||||||||||||
| 25 | = $var eq '.' ? $s->{template}{context}{scopes} | ||||||||||||||
| 26 | : $var eq '.*' ? [$s->{template}{context}{scopes}] | ||||||||||||||
| 27 | 1 | ▶ | : $s->{template}{context}->get($var); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 28 | 1 | ▶ | if (eval { require Data::Dump }) { # Better | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 1 | 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 | 2 | 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<extending Template::Liquid|Template::Liquid/"Extending Template::Liquid">. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | This tag attempts to use L<Data::Dump> and L<Data::Dumper> 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<Template::LiquidX::Tag::Include>. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head1 See Also | ||||||||||||||
| 86 | |||||||||||||||
| 87 | Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers | ||||||||||||||
| 88 | |||||||||||||||
| 89 | L<Template::Liquid|Template::Liquid/"Extending Template::Liquid">'s section on | ||||||||||||||
| 90 | custom tags. | ||||||||||||||
| 91 | |||||||||||||||
| 92 | =head1 Author | ||||||||||||||
| 93 | |||||||||||||||
| 94 | Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/ | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =head1 License and Legal | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Copyright (C) 2009-2016 by Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||||||||||||||
| 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<LICENSE> 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 | ||||||||||||||