package Template::Plugin::DataPrinter; use strict; use warnings; use base 'Template::Plugin'; # ABSTRACT: Template Toolkit dumper plugin using Data::Printer our $VERSION = '0.011'; # VERSION use HTML::FromANSI::Tiny (); use Hash::Merge::Simple qw< merge >; sub new { my ($class, $context, $params) = @_; require Data::Printer; my $dp_params = merge( { colored => 1, }, $params->{dp}); Data::Printer->import(%$dp_params); my $hfat_params = merge( { class_prefix => 'ansi_', no_plain_tags => 1, }, $params->{hfat}); my $hfat = HTML::FromANSI::Tiny->new(%$hfat_params); my $self = bless { _CONTEXT => $context, hfat => $hfat, }, $class; return $self; } sub dump { my $self = shift; my $text = join('', map { Data::Printer::np($_) . "\n" } @_); return $text; } sub dump_html { my $self = shift; my $html = $self->_css; my $text = $self->dump(@_); $html .= "
\n" . $self->{hfat}->html($text) . '
'; return $html; } sub _css { # Short of a better plan, emit the css on-demand before the first dump_html my $self = shift; return '' if $self->{done_css}; $self->{done_css} = 1; return $self->{hfat}->style_tag . "\n"; } 1; =pod =head1 NAME Template::Plugin::DataPrinter - Template Toolkit dumper plugin using Data::Printer =head1 VERSION version 0.011 =head1 SYNOPSIS [% USE DataPrinter %] [% DataPrinter.dump(variable) %] [% DataPrinter.dump_html(variable) %] =head1 DESCRIPTION This is a dumper plugin for L which uses L instead of L. L is a colorised pretty-printer with nicely human-readable object output. =head1 METHODS The provided methods match those of L. =head2 dump Generates an ansi-colorised dump of the data structures passed. [% USE DataPrinter %] [% DataPrinter.dump(myvar) %] [% DataPrinter.dump(myvar, yourvar) %] =head2 dump_html Generates a html-formatted dump of the data structures passed. The ansi colorisation is converted to html by L. [% USE DataPrinter %] [% DataPrinter.dump_html(myvar) %] [% DataPrinter.dump_html(myvar, yourvar) %] =head1 CONFIGURATION This plugin has no configuration of its own, but the underlying L and L modules can be configured using the C and C parameters. [% USE DataPrinter(dp = { ... }, hfat = { ... }) %] =over =item dp A hashref containing the params to be passed to C. See the L documentation for more information. =item hfat A hashref containing the params to be passed to Cnew>. See the L documentation for more information. =back =head2 Disabling colorisation Colorisation is turned on by default. To turn it off, use L's C parameter: [% USE DataPrinter(dp = { colored = 0 }) %] =head2 Using as a drop-in replacement for Template::Plugin::Dumper This module can be used more-or-less as a drop-in replacement for the default Dumper plugin by specifying an explicit plugin mapping to the C