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
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.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9366)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9338)
The Mikado Method by Ola Ellnestam Daniel Brolund(9303)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7846)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7399)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7310)
Grails in Action by Glen Smith Peter Ledbrook(7291)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7112)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5953)
Kotlin in Action by Dmitry Jemerov(4634)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(3857)
Functional Programming in JavaScript by Mantyla Dan(3720)
Learning Java by Patrick Niemeyer & Daniel Leuck(2867)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2833)
Mastering Python for Finance by Weiming James Ma;(2827)
WordPress Plugin Development Cookbook by Yannick Lefebvre(2583)
Mastering Java 9 by Dr. Edward Lavieri(2570)
Architecting Modern Java EE Applications: Designing lightweight, business-oriented enterprise applications in the age of cloud, containers, and Java EE 8 by Daschner Sebastian(2556)
Learning Concurrency in Python by Elliot Forbes(2548)