Perl Template Toolkit by Cross Dave Chamberlain Darren Wardley Andy & Dave Cross & Andy Wardley

Perl Template Toolkit by Cross Dave Chamberlain Darren Wardley Andy & Dave Cross & Andy Wardley

Author:Cross, Dave, Chamberlain, Darren, Wardley, Andy & Dave Cross & Andy Wardley [Darren Chamberlain]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / JavaScript
ISBN: 9781449313234
Publisher: O'Reilly Media, Inc.
Published: 2011-06-06T16:00:00+00:00


A Provider That Can Fetch Files over HTTP

A relatively common question on the mailing list is, “Can I fetch templates via HTTP?” The official Template Toolkit FAQ[17] explains that, yes, you can, simply by using Template::Provider::HTTP. The problem, though, is that Template::Provider::HTTP does not exist.

Template::Provider already does most of what we want, including caching. Template::Provider::HTTP simply needs to add an LWP::UserAgent instance and customize the fetching process to use URIs rather than filesystem paths:

package Template::Provider::HTTP; use strict; use vars qw($VERSION); use base qw(Template::Provider); $VERSION = 1.00; use File::Spec; use HTTP::Request::Common qw(HEAD GET); use LWP::UserAgent; use Template::Constants qw(:status); use Template::Provider; use URI; use URI::Escape qw(uri_escape);

In addition to Template::Provider and Template::Constants (for the STATUS constants), we need LWP::UserAgent, with which we will do the actual fetching, HTTP::Request::Common to create HTTP::Request objects (the GET and HEAD functions are very convenient shortcuts), and the URI, URI::Escape, and File::Spec modules to manipulate URIs and files.

When a Template::Provider::HTTP object is created, we need to also create an LWP::UserAgent instance. Template::Provider::_init already handles the caching parameters, so we call it from our own _init:

sub _init { my ($self, $params) = @_; my ($ua, %lwp_args, $lwp_arg); $self->SUPER::_init($params);

Now we can do the LWP initialization. This list contains all the constructor options that LWP knows about, but for the sake of consistency with the Template Toolkit’s native configuration methods, we require all uppercase option names:

for $lwp_arg (qw(agent from timeout use_eval parse_head max_size cookie_jar conn_cache protocols_allowed protocols_forbidden protocols_redirectable)) { my $uc_lwp_arg = uc $lwp_arg; $lwp_args{ $lwp_arg } = $params->{ $uc_lwp_arg } if defined $params->{ $uc_lwp_arg }; } $self->{ USERAGENT } = $ua = LWP::UserAgent->new(%lwp_args);

A busy web site using this provider might want to put a caching proxy between the application server and the server providing the templates (even with the caching, we still need to HEAD the URI to see if it has changed). Setting up LWP’s proxy support is simple:

if (my $proxy = $params->{ PROXY }) { $ua->proxy('http', $proxy); } if (my $no_proxy = $params->{ NO_PROXY }) { $no_proxy = [ $no_proxy ] unless ref($no_proxy) eq 'ARRAY'; $ua->no_proxy(@$no_proxy); }

The NO_PROXY option defines domains for which LWP should not use the proxy.

If we’re debugging the provider, we can turn on debugging in LWP as well, using LWP::Debug:

if ($self->{ DEBUG }) { require LWP::Debug; LWP::Debug::level('+'); }

And, for good measure, we uniquely identify this agent, so it can be specifically picked out by the logs:

$ua->agent(sprintf "%s [%s/%.02f]", $ua->_agent, ref($self), $VERSION);

Because we do not have a base filename to use when contructing paths for compiled versions of the templates, we need to have COMPILE_DIR set if COMPILE_EXT is set (otherwise, the provider will try to create directories in /; we’ll see this in more detail when we discuss _fetch).

# IF COMPILE_EXT is set, COMPILE_DIR must also be set my ($cdir, $cext) = @$params{ qw( COMPILE_DIR COMPILE_EXT ) }; if (length($cext) && ! length($cdir)) { return $self->error("COMPILE_DIR must be set if COMPILE_EXT is set"); } return $self; }

The main method of our provider, fetch, can be much simpler than the default fetch:

sub fetch { my



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.