.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Template::Service 3pm" .TH Template::Service 3pm "2011-12-20" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Template::Service \- General purpose template processing service .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Template::Service; \& \& my $service = Template::Service\->new({ \& PRE_PROCESS => [ \*(Aqconfig\*(Aq, \*(Aqheader\*(Aq ], \& POST_PROCESS => \*(Aqfooter\*(Aq, \& ERROR => { \& user => \*(Aquser/index.html\*(Aq, \& dbi => \*(Aqerror/database\*(Aq, \& default => \*(Aqerror/default\*(Aq, \& }, \& }); \& \& my $output = $service\->process($template_name, \e%replace) \& || die $service\->error(), "\en"; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The \f(CW\*(C`Template::Service\*(C'\fR module implements an object class for providing a consistent template processing service. .PP Standard header (\s-1PRE_PROCESS\s0) and footer (\s-1POST_PROCESS\s0) templates may be specified which are prepended and appended to all templates processed by the service (but not any other templates or blocks \f(CW\*(C`INCLUDE\*(C'\fRd or \f(CW\*(C`PROCESS\*(C'\fRed from within). An \&\s-1ERROR\s0 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. .PP A default \f(CW\*(C`Template::Service\*(C'\fR object is created by the Template module. Any \f(CW\*(C`Template::Service\*(C'\fR options may be passed to the Template \&\fInew()\fR constructor method and will be forwarded to the Template::Service constructor. .PP .Vb 1 \& use Template; \& \& my $template = Template\->new({ \& PRE_PROCESS => \*(Aqheader\*(Aq, \& POST_PROCESS => \*(Aqfooter\*(Aq, \& }); .Ve .PP Similarly, the \f(CW\*(C`Template::Service\*(C'\fR constructor will forward all configuration parameters onto other default objects (e.g. Template::Context) that it may need to instantiate. .PP A \f(CW\*(C`Template::Service\*(C'\fR object (or subclass) can be explicitly instantiated and passed to the Template \fInew()\fR constructor method as the \&\s-1SERVICE\s0 item. .PP .Vb 2 \& use Template; \& use Template::Service; \& \& my $service = Template::Service\->new({ \& PRE_PROCESS => \*(Aqheader\*(Aq, \& POST_PROCESS => \*(Aqfooter\*(Aq, \& }); \& \& my $template = Template\->new({ \& SERVICE => $service, \& }); .Ve .PP The \f(CW\*(C`Template::Service\*(C'\fR module can be sub-classed to create custom service handlers. .PP .Vb 2 \& use Template; \& use MyOrg::Template::Service; \& \& my $service = MyOrg::Template::Service\->new({ \& PRE_PROCESS => \*(Aqheader\*(Aq, \& POST_PROCESS => \*(Aqfooter\*(Aq, \& COOL_OPTION => \*(Aqenabled in spades\*(Aq, \& }); \& \& my $template = Template\->new({ \& SERVICE => $service, \& }); .Ve .PP The Template module uses the Template::Config \&\fIservice()\fR factory method to create a default service object when required. The \f(CW$Template::Config::SERVICE\fR package variable may be set to specify an alternate service module. This will be loaded automatically and its \fInew()\fR constructor method called by the \&\fIservice()\fR factory method when a default service object is required. Thus the previous example could be written as: .PP .Vb 1 \& use Template; \& \& $Template::Config::SERVICE = \*(AqMyOrg::Template::Service\*(Aq; \& \& my $template = Template\->new({ \& PRE_PROCESS => \*(Aqheader\*(Aq, \& POST_PROCESS => \*(Aqfooter\*(Aq, \& COOL_OPTION => \*(Aqenabled in spades\*(Aq, \& }); .Ve .SH "METHODS" .IX Header "METHODS" .SS "new(\e%config)" .IX Subsection "new(%config)" The \f(CW\*(C`new()\*(C'\fR constructor method is called to instantiate a \f(CW\*(C`Template::Service\*(C'\fR object. Configuration parameters may be specified as a \s-1HASH\s0 reference or as a list of \f(CW\*(C`name => value\*(C'\fR pairs. .PP .Vb 4 \& my $service1 = Template::Service\->new({ \& PRE_PROCESS => \*(Aqheader\*(Aq, \& POST_PROCESS => \*(Aqfooter\*(Aq, \& }); \& \& my $service2 = Template::Service\->new( ERROR => \*(Aqerror.html\*(Aq ); .Ve .PP The \f(CW\*(C`new()\*(C'\fR method returns a \f(CW\*(C`Template::Service\*(C'\fR object or \f(CW\*(C`undef\*(C'\fR on error. In the latter case, a relevant error message can be retrieved by the \&\fIerror()\fR class method or directly from the \&\f(CW$Template::Service::ERROR\fR package variable. .PP .Vb 2 \& my $service = Template::Service\->new(\e%config) \& || die Template::Service\->error(); \& \& my $service = Template::Service\->new(\e%config) \& || die $Template::Service::ERROR; .Ve .SS "process($input, \e%replace)" .IX Subsection "process($input, %replace)" The \f(CW\*(C`process()\*(C'\fR method is called to process a template specified as the first parameter, \f(CW$input\fR. This may be a file name, file handle (e.g. \f(CW\*(C`GLOB\*(C'\fR or \&\f(CW\*(C`IO::Handle\*(C'\fR) or a reference to a text string containing the template text. An additional hash reference may be passed containing template variable definitions. .PP The method processes the template, adding any \&\s-1PRE_PROCESS\s0 or \&\s-1POST_PROCESS\s0 templates defined, and returns the output text. An uncaught exception thrown by the template will be handled by a relevant \s-1ERROR\s0 handler if defined. Errors that occur in the \&\s-1PRE_PROCESS\s0 or \&\s-1POST_PROCESS\s0 templates, or those that occur in the main input template and aren't handled, cause the method to return \f(CW\*(C`undef\*(C'\fR to indicate failure. The appropriate error message can be retrieved via the \&\fIerror()\fR method. .PP .Vb 2 \& $service\->process(\*(Aqmyfile.html\*(Aq, { title => \*(AqMy Test File\*(Aq }) \& || die $service\->error(); .Ve .SS "\fIcontext()\fP" .IX Subsection "context()" Returns a reference to the internal context object which is, by default, an instance of the Template::Context class. .SH "CONFIGURATION OPTIONS" .IX Header "CONFIGURATION OPTIONS" The following list summarises the configuration options that can be provided to the \f(CW\*(C`Template::Service\*(C'\fR \fInew()\fR constructor. Please consult Template::Manual::Config for further details and examples of each configuration option in use. .SS "\s-1PRE_PROCESS\s0, \s-1POST_PROCESS\s0" .IX Subsection "PRE_PROCESS, POST_PROCESS" The \s-1PRE_PROCESS\s0 and \&\s-1POST_PROCESS\s0 options may be set to contain the name(s) of template files 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 \f(CW\*(C`INCLUDE\*(C'\fR \&\f(CW\*(C`PROCESS\*(C'\fR, \f(CW\*(C`WRAPPER\*(C'\fR, etc. .PP .Vb 4 \& my $service = Template::Service\->new({ \& PRE_PROCESS => \*(Aqheader\*(Aq, \& POST_PROCESS => \*(Aqfooter\*(Aq, \& }; .Ve .PP Multiple templates may be specified as a reference to a list. Each is processed in the order defined. .PP .Vb 4 \& my $service = Template::Service\->new({ \& PRE_PROCESS => [ \*(Aqconfig\*(Aq, \*(Aqheader\*(Aq ], \& POST_PROCESS => \*(Aqfooter\*(Aq, \& }; .Ve .SS "\s-1PROCESS\s0" .IX Subsection "PROCESS" The \s-1PROCESS\s0 option may be set to contain the name(s) of template files which should be processed instead of the main template passed to the \f(CW\*(C`Template::Service\*(C'\fR \fIprocess()\fR method. This can be used to apply consistent wrappers around all templates, similar to the use of \&\s-1PRE_PROCESS\s0 and \&\s-1POST_PROCESS\s0 templates. .PP .Vb 3 \& my $service = Template::Service\->new({ \& PROCESS => \*(Aqcontent\*(Aq, \& }; \& \& # processes \*(Aqcontent\*(Aq instead of \*(Aqfoo.html\*(Aq \& $service\->process(\*(Aqfoo.html\*(Aq); .Ve .PP A reference to the original template is available in the \f(CW\*(C`template\*(C'\fR variable. Metadata items can be inspected and the template can be processed by specifying it as a variable reference (i.e. prefixed by \&'\f(CW\*(C`$\*(C'\fR') to an \f(CW\*(C`INCLUDE\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR or \f(CW\*(C`WRAPPER\*(C'\fR directive. .PP Example \f(CW\*(C`PROCESS\*(C'\fR template: .PP .Vb 8 \& \& \& [% template.title %] \& \& \& [% PROCESS $template %] \& \& .Ve .SS "\s-1ERROR\s0" .IX Subsection "ERROR" The \s-1ERROR\s0 (or \f(CW\*(C`ERRORS\*(C'\fR 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. .PP If specified as a single value then that template will be processed for all uncaught exceptions. .PP .Vb 3 \& my $service = Template::Service\->new({ \& ERROR => \*(Aqerror.html\*(Aq \& }); .Ve .PP If the \s-1ERROR/ERRORS\s0 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 \f(CW\*(C`default\*(C'\fR template may be provided for the general case. .PP .Vb 7 \& my $service = Template::Service\->new({ \& ERRORS => { \& user => \*(Aquser/index.html\*(Aq, \& dbi => \*(Aqerror/database\*(Aq, \& default => \*(Aqerror/default\*(Aq, \& }, \& }); .Ve .SS "\s-1AUTO_RESET\s0" .IX Subsection "AUTO_RESET" The \s-1AUTO_RESET\s0 option is set by default and causes the local \f(CW\*(C`BLOCKS\*(C'\fR cache for the Template::Context object to be reset on each call to the Template \fIprocess()\fR method. This ensures that any \f(CW\*(C`BLOCK\*(C'\fRs defined within a template will only persist until that template is finished processing. .SS "\s-1DEBUG\s0" .IX Subsection "DEBUG" The \s-1DEBUG\s0 option can be used to enable debugging messages from the \f(CW\*(C`Template::Service\*(C'\fR module by setting it to include the \f(CW\*(C`DEBUG_SERVICE\*(C'\fR value. .PP .Vb 1 \& use Template::Constants qw( :debug ); \& \& my $template = Template\->new({ \& DEBUG => DEBUG_SERVICE, \& }); .Ve .SH "AUTHOR" .IX Header "AUTHOR" Andy Wardley .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 1996\-2007 Andy Wardley. All Rights Reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" Template, Template::Context