Verby::Action::Template - Action to process Template Toolkit files


Verby-Action-Template documentation Contained in the Verby-Action-Template distribution.

Index


Code Index:

NAME

Top

Verby::Action::Template - Action to process Template Toolkit files

SYNOPSIS

Top

	use Verby::Action::Template;

DESCRIPTION

Top

This Action, given a set of template data, will process Template Toolkit files and return their output.

METHODS

Top

do

Run the template.

template_vars

Construct the tempalte variables.

verify

Returns true if $c->output is a plain string a file by that name exists.

FIELDS

Top

template_options

A hash reference containing the default options for the TT constructor. Has DEBUG_UNDEF enabled, and ABSOLUTE set to true.

template_object

A lazy field, whose default value is defined as Template->new( $self->template_options ).

BUGS

Top

None that we are aware of. Of course, if you find a bug, let us know, and we will be sure to fix it.

CODE COVERAGE

Top

We use Devel::Cover to test the code coverage of the tests, please refer to COVERAGE section of the Verby module for more information.

SEE ALSO

Top

AUTHOR

Top

Yuval Kogman, <nothingmuch@woobling.org>

COPYRIGHT AND LICENSE

Top


Verby-Action-Template documentation Contained in the Verby-Action-Template distribution.

#!/usr/bin/perl

package Verby::Action::Template;
use Moose;

with qw/Verby::Action/;

our $VERSION = "0.04";

use Template;
use Template::Constants qw( :debug );

has template_options => (
	isa => "HashRef",
	is  => "rw",
	auto_deref => 1,
	default    => sub { return {
		ABSOLUTE => 1,
		DEBUG    => DEBUG_UNDEF,
	}},
);

has template_object => (
	isa => "Object",
	is  => "rw",
	default => sub {
		my $self = shift;
		Template->new( $self->template_options ),
	},
);

sub do {
	my ( $self, $c ) = @_;

	my $output   = $c->output;
	my $template = $c->template;

	$c->logger->info("templating '$template' into $output");

	my $t = $self->template_object;

	$t->process($template, $self->template_vars($c), $output)
		|| $c->logger->logdie("couldn't process template: " . $t->error);
}

sub template_vars {
	my ( $self, $c ) = @_;
	return { c => $c };
}

sub verify {
	my ( $self, $c ) = @_;

	my $output = $c->output;

	(defined($output) and not ref($output))
		? -e $output
		: undef;
}

__PACKAGE__

__END__