Template::Service (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
Template::Service - General purpose template processing serviceSYNOPSIS
use Template::Service; my $service = Template::Service->new({ PRE_PROCESS => [ 'config', 'header' ], POST_PROCESS => 'footer', ERROR => { user => 'user/index.html', dbi => 'error/database', default => 'error/default', }, }); my $output = $service->process($template_name, \%replace) || die $service->error(), "\n";
DESCRIPTION
The "Template::Service" module implements an object class for providing a consistent template processing service.Standard header (
A default "Template::Service" object is created by the Template module. Any "Template::Service" options may be passed to the Template new() constructor method and will be forwarded to the Template::Service constructor.
use Template; my $template = Template->new({ PRE_PROCESS => 'header', POST_PROCESS => 'footer', });
Similarly, the "Template::Service" constructor will forward all configuration parameters onto other default objects (e.g. Template::Context) that it may need to instantiate.
A "Template::Service" object (or subclass) can be explicitly instantiated and passed to the Template new() constructor method as the
use Template; use Template::Service; my $service = Template::Service->new({ PRE_PROCESS => 'header', POST_PROCESS => 'footer', }); my $template = Template->new({ SERVICE => $service, });
The "Template::Service" module can be sub-classed to create custom service handlers.
use Template; use MyOrg::Template::Service; my $service = MyOrg::Template::Service->new({ PRE_PROCESS => 'header', POST_PROCESS => 'footer', COOL_OPTION => 'enabled in spades', }); my $template = Template->new({ SERVICE => $service, });
The Template module uses the Template::Config service() factory method to create a default service object when required. The $Template::Config::SERVICE package variable may be set to specify an alternate service module. This will be loaded automatically and its new() constructor method called by the service() factory method when a default service object is required. Thus the previous example could be written as:
use Template; $Template::Config::SERVICE = 'MyOrg::Template::Service'; my $template = Template->new({ PRE_PROCESS => 'header', POST_PROCESS => 'footer', COOL_OPTION => 'enabled in spades', });
METHODS
new(\%config)
The "new()" constructor method is called to instantiate a "Template::Service" object. Configuration parameters may be specified as a
my $service1 = Template::Service->new({ PRE_PROCESS => 'header', POST_PROCESS => 'footer', }); my $service2 = Template::Service->new( ERROR => 'error.html' );
The "new()" method returns a "Template::Service" object or "undef" on error. In the latter case, a relevant error message can be retrieved by the error() class method or directly from the $Template::Service::ERROR package variable.
my $service = Template::Service->new(\%config) || die Template::Service->error(); my $service = Template::Service->new(\%config) || die $Template::Service::ERROR;
process($input, \%replace)
The "process()" method is called to process a template specified as the first parameter, $input. This may be a file name, file handle (e.g. "GLOB" or "IO::Handle") or a reference to a text string containing the template text. An additional hash reference may be passed containing template variable definitions.The method processes the template, adding any
$service->process('myfile.html', { title => 'My Test File' }) || die $service->error();
context()
Returns a reference to the internal context object which is, by default, an instance of the Template::Context class.CONFIGURATION OPTIONS
The following list summarises the configuration options that can be provided to the "Template::Service" new() constructor. Please consult Template::Manual::Config for further details and examples of each configuration option in use.PRE_PROCESS, POST_PROCESS
The
my $service = Template::Service->new({ PRE_PROCESS => 'header', POST_PROCESS => 'footer', };
Multiple templates may be specified as a reference to a list. Each is processed in the order defined.
my $service = Template::Service->new({ PRE_PROCESS => [ 'config', 'header' ], POST_PROCESS => 'footer', };
PROCESS
The
my $service = Template::Service->new({ PROCESS => 'content', }; # processes 'content' instead of 'foo.html' $service->process('foo.html');
A reference to the original template is available in the "template" variable. Metadata items can be inspected and the template can be processed by specifying it as a variable reference (i.e. prefixed by '"$"') to an "INCLUDE", "PROCESS" or "WRAPPER" directive.
Example "PROCESS" template:
<html> <head> <title>[% template.title %]</title> </head> <body> [% PROCESS $template %] </body> </html>
ERROR
The If specified as a single value then that template will be processed for all uncaught exceptions.
my $service = Template::Service->new({ ERROR => 'error.html' });
If the
my $service = Template::Service->new({ ERRORS => { user => 'user/index.html', dbi => 'error/database', default => 'error/default', }, });
AUTO_RESET
The DEBUG
The
use Template::Constants qw( :debug ); my $template = Template->new({ DEBUG => DEBUG_SERVICE, });
AUTHOR
Andy Wardley <abw@wardley.org> <wardley.org>COPYRIGHT
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
Template, Template::ContextPOD ERRORS
Hey! The above document had some coding errors, which are explained below:- Around line 517:
-
alternative text 'ERROR/ERRORS' contains non-escaped | or /