Template Toolkit Modules

Template::Service

Tutorial ][ Manual ][ Modules ][ Library ][ Tools ][ FAQ ][ Release ]

<-[ Template::Provider ][ Template::Stash ]->

General purpose template processing service

Table of Contents

SYNOPSIS

Index ][ Modules ][ Top ]
    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

Index ][ Modules ][ Top ]

The Template::Service module implements an object class for providing a consistent template processing service.

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.

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/derivative) can be explicitly instantiated and passed to the Template new() constructor method as the SERVICE item.

    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

Index ][ Modules ][ Top ]

new(\%config)

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 => value) pairs.

    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 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.

    my $service = Template::Service->new(\%config)
	|| die Template::Service->error();
    my $service = Template::Service->new(\%config)
	|| die $Template::Service::ERROR;

The following configuration items may be specified:

  • PRE_PROCESS, POST_PROCESS

    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.

        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',
        };

    Alternately, multiple template may be specified as a single string, delimited by ':'. This delimiter string can be changed via the DELIMITER option.

        my $service = Template::Service->new({
    	PRE_PROCESS  => 'config:header',
    	POST_PROCESS => 'footer',
        };

    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.

    config:

        [% # set some site-wide variables
           bgcolor = '#ffffff'
           version = 2.718
        %]

    header:

        [% DEFAULT title = 'My Funky Web Site' %]
        <html>
        <head>
        <title>[% title %]</title>
        </head>
        <body bgcolor="[% bgcolor %]">

    footer:

        <hr>
        Version [% version %]
        </body>
        </html>

    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.

        $service->process('mydoc.html', $vars);

    mydoc.html:

        [% META title = 'My Document Title' %]
        blah blah blah
        ...

    header:

        <html>
        <head>
        <title>[% template.title %]</title></head>
        <body bgcolor="[% bgcolor %]">
  • PROCESS

    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.

        my $service = Template::Service->new({
    	PROCESS  => 'content',
        };
        # Etion object when created,
    allowing the catcher to examine and use the output up to the point at
    which the exception was raised.
    

        $output .= 'blah blah blah';
        $output .= 'more rhubarb';
        $context->throw('yack', 'Too much yacking', \$output);

    catch($exception, \\)

    Catches an exception thrown, either as a reference to a Template::Exception object or some other value. In the latter case, the error string is promoted to a Template::Exception object of 'undef' type. This method also accepts a reference to the current output buffer which is passed to the Template::Exception constructor, or is appended to the output buffer stored in an existing Template::Exception object, if unique (i.e. not the same reference). By this process, the correct state of the output buffer can be reconstructed for simple or nested throws.

    define_block($name, $block)

    Adds a new block definition to the internal BLOCKS cache. The first argument should contain the name of the block and the second a reference to a Template::Document object or template sub-routine, or template text which is automatically compiled into a template sub-routine. Returns a true value (the sub-routine or Template::Document reference) on success or undef on failure. The relevant error message can be retrieved by calling the error() method.

    define_filter($name, \&filter, $is_dynamic)

    Adds a new filter definition by calling the store() method on each of the LOAD_FILTERS providers until accepted (in the usual case, this is accepted straight away by the one and only Template::Filters provider). The first argument should contain the name of the filter and the second a reference to a filter subroutine. The optional third argument can be set to any true value to indicate that the subroutine is a dynamic filter factory. Returns a true value or throws a 'filter' exception on error.

    localise(\%vars)

    Clones the stash to create a context with localised variables. Returns a reference to the newly cloned stash object which is also stored internally.

        $stash = $context->localise();

    delocalise()

    Restore the stash to its state prior to localisation.

        $stash = $context->delocalise();

    visit(\%blocks)

    This method is called by Template::Document objects immediately before they process their content. It is called to register any local BLOCK definitions with the context object so that they may be subsequently delivered on request.

    leave()

    Compliment to visit(), above. Called by Template::Document objects immediately after they process their content.

    reset()

    Clears the local BLOCKS cache of any BLOCK definitions. Any initial set of BLOCKS specified as a configuration item to the constructor will be reinstated.

    AUTOLOAD

    An AUTOLOAD method provides access to context configuration items.

        $stash     = $context->stash();
        $tflag     = $context->trim();
        $epflag    = $context->eval_perl();
        ...

AUTHOR

Index ][ Modules ][ Top ]

Andy Wardley <abw@wardley.org>

http://wardley.org/

VERSION

Index ][ Modules ][ Top ]

2.98, distributed as part of the Template Toolkit version 2.19, released on 27 April 2007.

COPYRIGHT

Index ][ Modules ][ Top ]
  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

Index ][ Modules ][ Top ]

Template, Template::Document, Template::Exception, Template::Filters, Template::Plugins, Template::Provider, Template::Service, Template::Stash


<-[ Template::Constants ][ Template::Document ]->
./usr/share/doc/libtemplate-perl-doc/html/Modules/Template/Service.html0000644000000000000000000004443611013663225025025 0ustar rootroot Template Toolkit Modules

Template Toolkit Modules

Template::Service

Tutorial ][ Manual ][ Modules ][ Library ][ Tools ][ FAQ ][ Release ]

<-[ Template::Provider ][ Template::Stash ]->

General purpose template processing service

Table of Contents

SYNOPSIS

Index ][ Modules ][ Top ]
    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->e