1#==============================================================================
2#
3# Template::Plugin::Dumper
4#
5# DESCRIPTION
6#
7# A Template Plugin to provide a Template Interface to Data::Dumper
8#
9# AUTHOR
10#   Simon Matthews <sam@tt2.org>
11#
12# COPYRIGHT
13#   Copyright (C) 2000 Simon Matthews.  All Rights Reserved
14#
15#   This module is free software; you can redistribute it and/or
16#   modify it under the same terms as Perl itself.
17#
18#==============================================================================
19
20package Template::Plugin::Dumper;
21
22use strict;
23use warnings;
24use base 'Template::Plugin';
25use Data::Dumper;
26
27our $VERSION = 2.70;
28our $DEBUG   = 0 unless defined $DEBUG;
29our @DUMPER_ARGS = qw( Indent Pad Varname Purity Useqq Terse Freezer
30                       Toaster Deepcopy Quotekeys Bless Maxdepth );
31our $AUTOLOAD;
32
33#==============================================================================
34#                      -----  CLASS METHODS -----
35#==============================================================================
36
37#------------------------------------------------------------------------
38# new($context, \@params)
39#------------------------------------------------------------------------
40
41sub new {
42    my ($class, $context, $params) = @_;
43    my ($key, $val);
44    $params ||= { };
45
46
47    foreach my $arg (@DUMPER_ARGS) {
48        no strict 'refs';
49        if (defined ($val = $params->{ lc $arg })
50            or defined ($val = $params->{ $arg })) {
51            ${"Data\::Dumper\::$arg"} = $val;
52        }
53    }
54
55    bless {
56        _CONTEXT => $context,
57    }, $class;
58}
59
60sub dump {
61    my $self = shift;
62    my $content = Dumper @_;
63    return $content;
64}
65
66
67sub dump_html {
68    my $self = shift;
69    my $content = Dumper @_;
70    for ($content) {
71        s/&/&amp;/g;
72        s/</&lt;/g;
73        s/>/&gt;/g;
74        s/\n/<br>\n/g;
75    }
76    return $content;
77}
78
791;
80
81__END__
82
83=head1 NAME
84
85Template::Plugin::Dumper - Plugin interface to Data::Dumper
86
87=head1 SYNOPSIS
88
89    [% USE Dumper %]
90
91    [% Dumper.dump(variable) %]
92    [% Dumper.dump_html(variable) %]
93
94=head1 DESCRIPTION
95
96This is a very simple Template Toolkit Plugin Interface to the L<Data::Dumper>
97module.  A C<Dumper> object will be instantiated via the following directive:
98
99    [% USE Dumper %]
100
101As a standard plugin, you can also specify its name in lower case:
102
103    [% USE dumper %]
104
105The C<Data::Dumper> C<Pad>, C<Indent> and C<Varname> options are supported
106as constructor arguments to affect the output generated.  See L<Data::Dumper>
107for further details.
108
109    [% USE dumper(Indent=0, Pad="<br>") %]
110
111These options can also be specified in lower case.
112
113    [% USE dumper(indent=0, pad="<br>") %]
114
115=head1 METHODS
116
117There are two methods supported by the C<Dumper> object.  Each will
118output into the template the contents of the variables passed to the
119object method.
120
121=head2 dump()
122
123Generates a raw text dump of the data structure(s) passed
124
125    [% USE Dumper %]
126    [% Dumper.dump(myvar) %]
127    [% Dumper.dump(myvar, yourvar) %]
128
129=head2 dump_html()
130
131Generates a dump of the data structures, as per L<dump()>, but with the
132characters E<lt>, E<gt> and E<amp> converted to their equivalent HTML
133entities and newlines converted to E<lt>brE<gt>.
134
135    [% USE Dumper %]
136    [% Dumper.dump_html(myvar) %]
137
138=head1 AUTHOR
139
140Simon Matthews E<lt>sam@tt2.orgE<gt>
141
142=head1 COPYRIGHT
143
144Copyright (C) 2000 Simon Matthews.  All Rights Reserved.
145
146This module is free software; you can redistribute it and/or
147modify it under the same terms as Perl itself.
148
149=head1 SEE ALSO
150
151L<Template::Plugin>, L<Data::Dumper>
152
153