I find that when I separate my presentation and code with Template Toolkit, I often find that I've goofed in trying to access my data correctly within the template. This little snippet generates a nice, pretty-printed output directly to the template.

To use this, I place the following line of code right before the closing body tag in the template:

<pre>[% dump %]</pre>
use strict; use CGI; use Template; use constant DEBUGGING => 1; $|++; my $query = CGI::->new; my $template = Template->new( { INCLUDE_PATH => '..\templates', ABSOLUTE => 1 } ); my $data = { one => 1, two => 'foo', bar => [qw/ baz qux Ovid/] }; if ( DEBUGGING ) { require Data::Dumper; require HTML::Entities; $data->{ dump } = HTML::Entities::encode_entities( Data::Dumper::Dumper( $data ) + ); } print $query->header; $template->process( 'some.tmpl', $data ) or dienice $template->error();

Replies are listed 'Best First'.
•Re: Debugging Template Toolkit
by merlyn (Sage) on Feb 21, 2002 at 01:35 UTC
    I'm surprised you hadn't discovered Template::Plugin::Dumper yet! For example, in my testing template, I have:
    [% USE Dumper; global.foo = global.foo + 1; "<pre>"; "global is:\n"; Dumper.dump(global) | html; "</pre>"; component.foo = component.foo + 1; "<pre>"; "component is:\n"; Dumper.dump(component) | html; "</pre>"; template.foo = template.foo + 1; "<pre>"; "template is:\n"; Dumper.dump(template) | html; "</pre>"; %] [% "<pre>"; USE dumper(indent = 1); dumper.dump(uri, env, params, pnotes, cookies) | html "</pre>"; %]

    -- Randal L. Schwartz, Perl hacker