#============================================================= -*-Perl-*- # # Template::Service # # DESCRIPTION # Module implementing a template processing service which wraps a # template within PRE_PROCESS and POST_PROCESS templates and offers # ERROR recovery. # # AUTHOR # Andy Wardley # # COPYRIGHT # Copyright (C) 1996-2022 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. # #============================================================================ package Template::Service; use strict; use warnings; use base 'Template::Base'; use Template::Config; use Template::Exception; use Template::Constants; use Scalar::Util 'blessed'; use constant EXCEPTION => 'Template::Exception'; our $VERSION = '3.100'; our $DEBUG = 0 unless defined $DEBUG; our $ERROR = ''; #======================================================================== # ----- PUBLIC METHODS ----- #======================================================================== #------------------------------------------------------------------------ # process($template, \%params) # # Process a template within a service framework. A service may encompass # PRE_PROCESS and POST_PROCESS templates and an ERROR hash which names # templates to be substituted for the main template document in case of # error. Each service invocation begins by resetting the state of the # context object via a call to reset(). The AUTO_RESET option may be set # to 0 (default: 1) to bypass this step. #------------------------------------------------------------------------ sub process { my ($self, $template, $params) = @_; my $context = $self->{ CONTEXT }; my ($name, $output, $procout, $error); $output = ''; $self->debug( "process($template, ", defined $params ? $params : '', ')' ) if $self->{ DEBUG }; $context->reset() if $self->{ AUTO_RESET }; # pre-request compiled template from context so that we can alias it # in the stash for pre-processed templates to reference eval { $template = $context->template($template) }; return $self->error($@) if $@; # localise the variable stash with any parameters passed # and set the 'template' variable $params ||= { }; # TODO: change this to C<||=> so we can use a template parameter $params->{ template } = $template unless ref $template eq 'CODE'; $context->localise($params); SERVICE: { # PRE_PROCESS eval { foreach $name (@{ $self->{ PRE_PROCESS } }) { $self->debug("PRE_PROCESS: $name") if $self->{ DEBUG }; $output .= $context->process($name); } }; last SERVICE if ($error = $@); # PROCESS eval { foreach $name (@{ $self->{ PROCESS } || [ $template ] }) { $self->debug("PROCESS: $name") if $self->{ DEBUG }; $procout .= $context->process($name); } }; if ($error = $@) { last SERVICE unless defined ($procout = $self->_recover(\$error)); } if (defined $procout) { # WRAPPER eval { foreach $name (reverse @{ $self->{ WRAPPER } }) { $self->debug("WRAPPER: $name") if $self->{ DEBUG }; $procout = $context->process($name, { content => $procout }); } }; last SERVICE if ($error = $@); $output .= $procout; } # POST_PROCESS eval { foreach $name (@{ $self->{ POST_PROCESS } }) { $self->debug("POST_PROCESS: $name") if $self->{ DEBUG }; $output .= $context->process($name); } }; last SERVICE if ($error = $@); } $context->delocalise(); delete $params->{ template }; if ($error) { # $error = $error->as_string if ref $error; return $self->error($error); } return $output; } #------------------------------------------------------------------------ # context() # # Returns the internal CONTEXT reference. #------------------------------------------------------------------------ sub context { return $_[0]->{ CONTEXT }; } #======================================================================== # -- PRIVATE METHODS -- #======================================================================== sub _init { my ($self, $config) = @_; my ($item, $data, $context, $block, $blocks); my $delim = $config->{ DELIMITER }; $delim = ':' unless defined $delim; # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary, # by splitting on non-word characters foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) { $data = $config->{ $item }; $self->{ $item } = [ ], next unless (defined $data); $data = [ split($delim, $data || '') ] unless ref $data eq 'ARRAY'; $self->{ $item } = $data; } # unset PROCESS option unless explicitly specified in config $self->{ PROCESS } = undef unless defined $config->{ PROCESS }; $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS }; $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET } ? $config->{ AUTO_RESET } : 1; $self->{ DEBUG } = ( $config->{ DEBUG } || 0 ) & Template::Constants::DEBUG_SERVICE; $context = $self->{ CONTEXT } = $config->{ CONTEXT } || Template::Config->context($config) || return $self->error(Template::Config->error); return $self; } #------------------------------------------------------------------------ # _recover(\$exception) # # Examines the internal ERROR hash array to find a handler suitable # for the exception object passed by reference. Selecting the handler # is done by delegation to the exception's select_handler() method, # passing the set of handler keys as arguments. A 'default' handler # may also be provided. The handler value represents the name of a # template which should be processed. #------------------------------------------------------------------------ sub _recover { my ($self, $error) = @_; my $context = $self->{ CONTEXT }; my ($hkey, $handler, $output); # there shouldn't ever be a non-exception object received at this # point... unless a module like CGI::Carp messes around with the # DIE handler. return undef unless blessed($$error) && $$error->isa(EXCEPTION); # a 'stop' exception is thrown by [% STOP %] - we return the output # buffer stored in the exception object return $$error->text() if $$error->type() eq 'stop'; my $handlers = $self->{ ERROR } || return undef; ## RETURN if (ref $handlers eq 'HASH') { if ($hkey = $$error->select_handler(keys %$handlers)) { $handler = $handlers->{ $hkey }; $self->debug("using error handler for $hkey") if $self->{ DEBUG }; } elsif ($handler = $handlers->{ default }) { # use default handler $self->debug("using default error handler") if $self->{ DEBUG }; } else { return undef; ## RETURN } } else { $handler = $handlers; $self->debug("using default error handler") if $self->{ DEBUG }; } eval { $handler = $context->template($handler) }; if ($@) { $$error = $@; return undef; ## RETURN }; $context->stash->set('error', $$error); eval { $output .= $context->process($handler); }; if ($@) { $$error = $@; return undef; ## RETURN } return $output; } 1; __END__ =head1 NAME Template::Service - General purpose template processing service =head1 SYNOPSIS 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"; =head1 DESCRIPTION The C module implements an object class for providing a consistent template processing service. Standard header (L) and footer (L) templates may be specified which are prepended and appended to all templates processed by the service (but not any other templates or blocks Cd or Ced from within). An L 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. A default C object is created by the L