The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
[%#
  # IMPORTANT NOTE
  #   This documentation is generated automatically from source
  #   templates.  Any changes you make here may be lost.
  # 
  #   The 'docsrc' documentation source bundle is available for download
  #   from http://www.template-toolkit.org/docs.html and contains all
  #   the source templates, XML files, scripts, etc., from which the
  #   documentation for the Template Toolkit is built.
-%]
[% META book = 'Modules'
        page = 'Service'
%]
[%  WRAPPER toc;
	PROCESS tocitem 
	        title ="SYNOPSIS"
                subs  = [];
	PROCESS tocitem 
	        title ="DESCRIPTION"
                subs  = [];
	PROCESS tocitem 
	        title ="METHODS"
                subs  = [
                    "new(\\%config)",
		    "process(\$input, \\%replace)",
		    "context()",
		    "error()"
		];
	PROCESS tocitem 
	        title ="AUTHOR"
                subs  = [];
	PROCESS tocitem 
	        title ="VERSION"
                subs  = [];
	PROCESS tocitem 
	        title ="COPYRIGHT"
                subs  = [];
	PROCESS tocitem 
	        title ="SEE ALSO"
                subs  = [];
    END
%]
<!-- Pod to HTML conversion by the Template Toolkit version 2 -->
[% WRAPPER section
    title="SYNOPSIS"
-%]<pre>    use Template::Service;</pre>
<pre>    my $service = Template::Service-&gt;new({
	PRE_PROCESS  =&gt; [ 'config', 'header' ],
	POST_PROCESS =&gt; 'footer',
	ERROR        =&gt; {
	    user     =&gt; 'user/index.html', 
	    dbi      =&gt; 'error/database',
	    default  =&gt; 'error/default',
	},
    });</pre>
<pre>    my $output = $service-&gt;process($template_name, \%replace)
	|| die $service-&gt;error(), &quot;\n&quot;;</pre>
[%- END %]
[% WRAPPER section
    title="DESCRIPTION"
-%]<p>
The Template::Service module implements an object class for providing
a consistent template processing service. 
</p>
<p>
Standard header (PRE_PROCESS) and footer (POST_PROCESS) templates may
be specified which are prepended and appended to all templates
processed by the service (but not any other templates or blocks
INCLUDEd or PROCESSed from within).  An ERROR hash may be specified
which redirects the service to an alternate template file in the case
of uncaught exceptions being thrown.  This allows errors to be
automatically handled by the service and a guaranteed valid response
to be generated regardless of any processing problems encountered.
</p>
<p>
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.
</p>
<pre>    use Template;
    
    my $template = Template-&gt;new({
	PRE_PROCESS  =&gt; 'header',
	POST_PROCESS =&gt; 'footer',
    });</pre>
<p>
Similarly, the Template::Service constructor will forward all configuration
parameters onto other default objects (e.g. Template::Context) that it may 
need to instantiate.
</p>
<p>
A Template::Service object (or subclass/derivative) can be explicitly
instantiated and passed to the Template new() constructor method as 
the SERVICE item.
</p>
<pre>    use Template;
    use Template::Service;</pre>
<pre>    my $service = Template::Service-&gt;new({
	PRE_PROCESS  =&gt; 'header',
	POST_PROCESS =&gt; 'footer',
    });</pre>
<pre>    my $template = Template-&gt;new({
	SERVICE =&gt; $service,
    });</pre>
<p>
The Template::Service module can be sub-classed to create custom service
handlers.
</p>
<pre>    use Template;
    use MyOrg::Template::Service;</pre>
<pre>    my $service = MyOrg::Template::Service-&gt;new({
	PRE_PROCESS  =&gt; 'header',
	POST_PROCESS =&gt; 'footer',
	COOL_OPTION  =&gt; 'enabled in spades',
    });</pre>
<pre>    my $template = Template-&gt;new({
	SERVICE =&gt; $service,
    });</pre>
<p>
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:
</p>
<pre>    use Template;</pre>
<pre>    $Template::Config::SERVICE = 'MyOrg::Template::Service';</pre>
<pre>    my $template = Template-&gt;new({
	PRE_PROCESS  =&gt; 'header',
	POST_PROCESS =&gt; 'footer',
	COOL_OPTION  =&gt; 'enabled in spades',
    });</pre>
[%- END %]
[% WRAPPER section
    title="METHODS"
-%][% WRAPPER subsection
   title = "new(\\%config)"
-%]<p>
The new() constructor method is called to instantiate a Template::Service
object.  Configuration parameters may be specified as a HASH reference or
as a list of (name =&gt; value) pairs.
</p>
<pre>    my $service1 = Template::Service-&gt;new({
	PRE_PROCESS  =&gt; 'header',
	POST_PROCESS =&gt; 'footer',
    });</pre>
<pre>    my $service2 = Template::Service-&gt;new( ERROR =&gt; 'error.html' );</pre>
<p>
The new() method returns a Template::Service object (or sub-class) 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.
</p>
<pre>    my $service = Template::Service-&gt;new(\%config)
	|| die Template::Service-&gt;error();</pre>
<pre>    my $service = Template::Service-&gt;new(\%config)
	|| die $Template::Service::ERROR;</pre>
<p>
The following configuration items may be specified:
</p>
<ul>
<li><b>PRE_PROCESS, POST_PROCESS</b><br>
<p>
These values may be set to contain the name(s) of template files
(relative to INCLUDE_PATH) which should be processed immediately
before and/or after each template.  These do not get added to 
templates processed into a document via directives such as INCLUDE, 
PROCESS, WRAPPER etc.
</p>
<pre>    my $service = Template::Service-&gt;new({
	PRE_PROCESS  =&gt; 'header',
	POST_PROCESS =&gt; 'footer',
    };</pre>
<p>
Multiple templates may be specified as a reference to a list.  Each is 
processed in the order defined.
</p>
<pre>    my $service = Template::Service-&gt;new({
	PRE_PROCESS  =&gt; [ 'config', 'header' ],
	POST_PROCESS =&gt; 'footer',
    };</pre>
<p>
Alternately, multiple template may be specified as a single string, 
delimited by ':'.  This delimiter string can be changed via the 
DELIMITER option.
</p>
<pre>    my $service = Template::Service-&gt;new({
	PRE_PROCESS  =&gt; 'config:header',
	POST_PROCESS =&gt; 'footer',
    };</pre>
<p>
The PRE_PROCESS and POST_PROCESS templates are evaluated in the same
variable context as the main document and may define or update
variables for subsequent use.
</p>
<p>
config:
</p>
<pre>    [% tt_start_tag %] # set some site-wide variables
       bgcolor = '#ffffff'
       version = 2.718
    [% tt_end_tag %]</pre>
<p>
header:
</p>
<pre>    [% tt_start_tag %] DEFAULT title = 'My Funky Web Site' [% tt_end_tag %]
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;[% tt_start_tag %] title [% tt_end_tag %]&lt;/title&gt;
    &lt;/head&gt;
    &lt;body bgcolor=&quot;[% tt_start_tag %] bgcolor [% tt_end_tag %]&quot;&gt;</pre>
<p>
footer:
</p>
<pre>    &lt;hr&gt;
    Version [% tt_start_tag %] version [% tt_end_tag %]
    &lt;/body&gt;
    &lt;/html&gt;</pre>
<p>
The Template::Document object representing the main template being processed
is available within PRE_PROCESS and POST_PROCESS templates as the 'template'
variable.  Metadata items defined via the META directive may be accessed 
accordingly.
</p>
<pre>    $service-&gt;process('mydoc.html', $vars);</pre>
<p>
mydoc.html:
</p>
<pre>    [% tt_start_tag %] META title = 'My Document Title' [% tt_end_tag %]
    blah blah blah
    ...</pre>
<p>
header:
</p>
<pre>    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;[% tt_start_tag %] template.title [% tt_end_tag %]&lt;/title&gt;&lt;/head&gt;
    &lt;body bgcolor=&quot;[% tt_start_tag %] bgcolor [% tt_end_tag %]&quot;&gt;</pre>

<li><b>PROCESS</b><br>
<p>
The PROCESS option may be set to contain the name(s) of template files
(relative to INCLUDE_PATH) which should be processed instead of the 
main template passed to the Template::Service process() method.  This can 
be used to apply consistent wrappers around all templates, similar to 
the use of PRE_PROCESS and POST_PROCESS templates.
</p>
<pre>    my $service = Template::Service-&gt;new({
	PROCESS  =&gt; 'content',
    };</pre>
<pre>    # processes 'content' instead of 'foo.html'
    $service-&gt;process('foo.html');</pre>
<p>
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.
</p>
<p>
content:
</p>
<pre>    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;[% tt_start_tag %] template.title [% tt_end_tag %]&lt;/title&gt;
    &lt;/head&gt;
    
    &lt;body&gt;
    [% tt_start_tag %] PROCESS $template [% tt_end_tag %]
    &lt;hr&gt;
    &amp;copy; Copyright [% tt_start_tag %] template.copyright [% tt_end_tag %]
    &lt;/body&gt;
    &lt;/html&gt;</pre>
<p>
foo.html:
</p>
<pre>    [% tt_start_tag %] META 
       title     = 'The Foo Page'
       author    = 'Fred Foo'
       copyright = '2000 Fred Foo'
    [% tt_end_tag %]
    &lt;h1&gt;[% tt_start_tag %] template.title [% tt_end_tag %]&lt;/h1&gt;
    Welcome to the Foo Page, blah blah blah</pre>
<p>
output:    
</p>
<pre>    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;The Foo Page&lt;/title&gt;
    &lt;/head&gt;</pre>
<pre>    &lt;body&gt;
    &lt;h1&gt;The Foo Page&lt;/h1&gt;
    Welcome to the Foo Page, blah blah blah
    &lt;hr&gt;
    &amp;copy; Copyright 2000 Fred Foo
    &lt;/body&gt;
    &lt;/html&gt;</pre>

<li><b>ERROR</b><br>
<p>
The ERROR (or ERRORS if you prefer) configuration item can be used to
name a single template or specify a hash array mapping exception types
to templates which should be used for error handling.  If an uncaught
exception is raised from within a template then the appropriate error
template will instead be processed.
</p>
<p>
If specified as a single value then that template will be processed 
for all uncaught exceptions. 
</p>
<pre>    my $service = Template::Service-&gt;new({
	ERROR =&gt; 'error.html'
    });</pre>
<p>
If the ERROR item is a hash reference the keys are assumed to be
exception types and the relevant template for a given exception will
be selected.  A 'default' template may be provided for the general
case.  Note that 'ERROR' can be pluralised to 'ERRORS' if you find
it more appropriate in this case.
</p>
<pre>    my $service = Template::Service-&gt;new({
	ERRORS =&gt; {
	    user     =&gt; 'user/index.html',
	    dbi      =&gt; 'error/database',
	    default  =&gt; 'error/default',
	},
    });</pre>
<p>
In this example, any 'user' exceptions thrown will cause the
'user/index.html' template to be processed, 'dbi' errors are handled
by 'error/database' and all others by the 'error/default' template.
Any PRE_PROCESS and/or POST_PROCESS templates will also be applied
to these error templates.
</p>
<p>
Note that exception types are hierarchical and a 'foo' handler will
catch all 'foo.*' errors (e.g. foo.bar, foo.bar.baz) if a more
specific handler isn't defined.  Be sure to quote any exception types
that contain periods to prevent Perl concatenating them into a single
string (i.e. <code>'user.passwd'</code> is parsed as 'user'.'passwd').
</p>
<pre>    my $service = Template::Service-&gt;new({
	ERROR =&gt; {
	    'user.login'  =&gt; 'user/login.html',
	    'user.passwd' =&gt; 'user/badpasswd.html',
	    'user'        =&gt; 'user/index.html',
	    'default'     =&gt; 'error/default',
	},
    });</pre>
<p>
In this example, any template processed by the $service object, or
other templates or code called from within, can raise a 'user.login'
exception and have the service redirect to the 'user/login.html'
template.  Similarly, a 'user.passwd' exception has a specific 
handling template, 'user/badpasswd.html', while all other 'user' or
'user.*' exceptions cause a redirection to the 'user/index.html' page.
All other exception types are handled by 'error/default'.
</p>
<p>
Exceptions can be raised in a template using the THROW directive,
</p>
<pre>    [% tt_start_tag %] THROW user.login 'no user id: please login' [% tt_end_tag %]</pre>
<p>
or by calling the throw() method on the current Template::Context object,
</p>
<pre>    $context-&gt;throw('user.passwd', 'Incorrect Password');
    $context-&gt;throw('Incorrect Password');    # type 'undef'</pre>
<p>
or from Perl code by calling die() with a Template::Exception object,
</p>
<pre>    die Template::Exception-&gt;new('user.denied', 'Invalid User ID');</pre>
<p>
or by simply calling die() with an error string.  This is
automagically caught and converted to an  exception of 'undef'
type which can then be handled in the usual way.
</p>
<pre>    die &quot;I'm sorry Dave, I can't do that&quot;;</pre>

<li><b>AUTO_RESET</b><br>
<p>
The AUTO_RESET option is set by default and causes the local BLOCKS
cache for the Template::Context object to be reset on each call to the
Template process() method.  This ensures that any BLOCKs defined
within a template will only persist until that template is finished
processing.  This prevents BLOCKs defined in one processing request
from interfering with other independent requests subsequently
processed by the same context object.
</p>
<p>
The BLOCKS item may be used to specify a default set of block definitions
for the Template::Context object.  Subsequent BLOCK definitions in templates
will over-ride these but they will be reinstated on each reset if AUTO_RESET
is enabled (default), or if the Template::Context reset() method is called.
</p>

</ul>
[%- END %]
[% WRAPPER subsection
   title = "process(\$input, \\%replace)"
-%]<p>
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.
</p>
<p>
The method processes the template, adding any PRE_PROCESS or POST_PROCESS 
templates defined, and returns the output text.  An uncaught exception thrown 
by the template will be handled by a relevant ERROR handler if defined.
Errors that occur in the PRE_PROCESS or POST_PROCESS templates, or those that
occur in the main input template and aren't handled, cause the method to 
return undef to indicate failure.  The appropriate error message can be
retrieved via the error() method.
</p>
<pre>    $service-&gt;process('myfile.html', { title =&gt; 'My Test File' })
	|| die $service-&gt;error();</pre>
[%- END %]
[% WRAPPER subsection
   title = "context()"
-%]<p>
Returns a reference to the internal context object which is, by default, an
instance of the Template::Context class.
</p>
[%- END %]
[% WRAPPER subsection
   title = "error()"
-%]<p>
Returns the most recent error message.
</p>
[%- END %]
[%- END %]
[% WRAPPER section
    title="AUTHOR"
-%]<p>
Andy Wardley &lt;abw@andywardley.com&gt;
</p>
<p>
[% ttlink('http://www.andywardley.com/', 'http://www.andywardley.com/') -%]
</p>
[%- END %]
[% WRAPPER section
    title="VERSION"
-%]<p>
2.61, distributed as part of the
Template Toolkit version 2.08, released on 30 July 2002.
</p>
[%- END %]
[% WRAPPER section
    title="COPYRIGHT"
-%]<pre>  Copyright (C) 1996-2002 Andy Wardley.  All Rights Reserved.
  Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.</pre>
<p>
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
</p>
[%- END %]
[% WRAPPER section
    title="SEE ALSO"
-%]<p>
[% ttlink('Template', 'Template') -%], [% ttlink('Template::Context', 'Template::Context') -%]
</p>
[%- END %]