| Template-Process documentation | Contained in the Template-Process distribution. |
Template::Process - Process TT2 templates against data files
use Template::Process;
$tt = Template::Process->new();
# VARS
$tt->process(TT => 'h.tt.html', DATA => 'vars.yml', OUT => 'h.html')
or die $tt->error;
This module implements a facility to process TT2 scripts against data files, so that applying simple templates to simple data involves no coding.
This is the heart of the tt script (which comes in the same distribution).
$tt = Template::Process->new;
The constructor.
$tt->process(TT => $tt, DATA => \@data, OUT => $out);
The elements at @data may be: hash refs, YAML and Perl filenames.
A YAML filename is expected to exist (-f $_ returns true)
and match /\.ya?ml$/. A Perl filename must satisfy -f $_
and match /\.PL$/.
If DATA is ommitted, the template is processed with no
extra variables.
If OUT is ommitted, \*STDOUT is used.
$tt->process(@ARGS) or
die $tt->error;
In case of processing errors, returns a (hopefully) helpful message.
None at all. This is OO.
ttree (from Template-Toolkit distribution)
Adriano Ferreira, <ferreira@cpan.org>
Copyright (C) 2006-2007 by Adriano Ferreira
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Template-Process documentation | Contained in the Template-Process distribution. |
package Template::Process; use 5; use strict; use warnings; our $VERSION = '0.0006'; $VERSION = eval $VERSION; use base qw(Class::Accessor); Template::Process->mk_accessors(qw(tt)); use Carp qw(carp croak); use Template; use Data::IO; sub new { return shift->SUPER::new({ tt => Template->new }); }
sub _process { my $self = shift; my %args = @_; # warn YAML::Dump \%args; my $tt = $args{TT}; my $out = $args{OUT} || \*STDOUT; my @data; @data = ref $args{DATA} ? @{$args{DATA}} : ($args{DATA}); my %data; %data = (%data, %$_) for @data; return $self->tt->process($tt, \%data, $out); } sub _io { # this works only for $] >= 5.8 open my $io, '>', shift or croak "can't open in-core file: $!"; return $io; } sub process { my $self = shift; my %args = @_; # warn YAML::Dump \%args; my $tt = $args{TT}; my $reader = Data::IO->new; my @data; my @args = defined $args{DATA} ? (ref $args{DATA} ? @{$args{DATA}} : ($args{DATA})) : (); for (@args) { if (ref $_) { # perl data already push @data, $_; } elsif (-f && /\.ya?ml$/) { push @data, $reader->_read_yaml($_); } elsif (-f && /\.PL$/i) { push @data, $reader->_read_perl($_); } else { carp "'$_' ignored: unkown format\n"; } } my $out = $args{OUT}; $out = _io($out) if (ref $out eq 'SCALAR'); $out = \*STDOUT unless $out; return $self->_process(TT => $tt, DATA => \@data, OUT => $out); } sub error { return shift->tt->error } 1; __END__