CGI-Application-Plugin-AnyTemplate
view release on metacpan or search on metacpan
lib/CGI/Application/Plugin/AnyTemplate.pm view on Meta::CPAN
);
}
sub old_style_runmode {
my $self = shift;
# ...
# use TemplateToolkit to fill template edit_user.tmpl
$self->template('oldstyle')->fill('edit_user', \%params);
}
sub new_style_runmode {
my $self = shift;
# ...
# use Petal to fill template edit_user.xhml
$self->template->fill('edit_user.xhtml', \%params);
}
=head2 Flexible Syntax
The syntax is pretty flexible. Pick a style that's most comfortable for
you.
=head3 CGI::Application::Plugin::TT style syntax
$self->template->process('edit_user', \%params);
or (with slightly less typing):
$self->template->fill('edit_user', \%params);
=head3 CGI::Application load_tmpl style syntax
my $template = $self->template->load('edit_user');
$template->param('foo' => 'bar');
$template->output;
=head3 Verbose syntax (for complete control)
my $template = $self->template('named_config')->load(
file => 'edit_user'
type => 'TemplateToolkit'
add_include_paths => '.',
);
$template->param('foo' => 'bar');
$template->output;
See also below under L<"CHANGING THE NAME OF THE 'template' METHOD">.
=cut
use strict;
use CGI::Application;
use Carp;
use Scalar::Util qw(weaken);
if ( ! eval { require Clone } ) {
if ( eval { require Clone::PP } ) {
no strict 'refs';
*Clone::clone = *Clone::PP::clone;
}
else {
die "Neiter Clone nor Clone::PP found - $@\n";
}
}
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $CAPAT_Namespace);
@ISA = ('Exporter');
@ISA = 'Exporter';
@EXPORT = qw(template);
@EXPORT_OK = qw(load_tmpl);
%EXPORT_TAGS = (load_tmpl => ['load_tmpl', @EXPORT]);
$CAPAT_Namespace = '__ANY_TEMPLATE';
if (CGI::Application->can('new_hook')) {
CGI::Application->new_hook('template_pre_process');
CGI::Application->new_hook('template_post_process');
CGI::Application->new_hook('load_tmpl');
}
sub _new {
my $proto = shift;
my $class = ref $proto || $proto;
my $webapp = shift;
my $conf_name = shift;
my $self = {
'conf_name' => $conf_name,
'base_config' => {},
'current_config' => {},
'webapp' => $webapp,
};
bless $self, $class;
weaken $self->{'webapp'};
return $self;
}
sub _default_type { 'HTMLTemplate' }
sub _default_extension { '.html' }
=head1 METHODS
=head2 config
Initialize the C<AnyTemplate> system and provide the default
configuration.
$self->template->config(
default_type => 'HTMLTemplate',
);
You can keep multiple configurations handy at the same time by passing a
value to C<template>:
$self->template('oldstyle')->config(
default_type => 'HTML::Template',
);
$self->template('newstyle')->config(
default_type => 'HTML::Template::Expr',
);
Then in a runmode you can mix and match configurations:
$self->template('oldstyle')->load # loads an HTML::Template driver object
$self->template('newstyle')->load # loads an HTML::Template::Expr driver object
The configuration passed to C<config> is divided into three areas:
I<plugin configuration>, I<driver configuration>, and I<native
configuration>:
Config Type What it Configures
----------- ------------------
Plugin Config AnyTemplate itself
Driver Config AnyTemplate Driver (e.g. HTMLTemplate)
Native Config Actual template module (e.g. HTML::Template)
These are described in more detail below.
=head3 Plugin Configuration
These configuration params are specific to the C<CGI::Application::Plugin::AnyTemplate> itself.
They are included at the top level of the configuration hash passed to C<config>. For instance:
$self->template->config(
default_type => 'HTMLTemplate',
auto_add_template_extension => 0,
);
The I<plugin configuration> parameters and their defaults are as follows:
=over 4
( run in 1.849 second using v1.01-cache-2.11-cpan-39bf76dae61 )