1 #============================================================= -*-perl-*-
6 # Template Toolkit configuration module.
9 # Andy Wardley <abw@wardley.org>
12 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
17 #========================================================================
19 package Template::Config;
23 use base 'Template::Base';
24 use vars qw( $VERSION $DEBUG $ERROR $INSTDIR
25 $PARSER $PROVIDER $PLUGINS $FILTERS $ITERATOR
26 $LATEX_PATH $PDFLATEX_PATH $DVIPS_PATH
27 $STASH $SERVICE $CONTEXT $CONSTANTS @PRELOAD );
30 $DEBUG = 0 unless defined $DEBUG;
32 $CONTEXT = 'Template::Context';
33 $FILTERS = 'Template::Filters';
34 $ITERATOR = 'Template::Iterator';
35 $PARSER = 'Template::Parser';
36 $PLUGINS = 'Template::Plugins';
37 $PROVIDER = 'Template::Provider';
38 $SERVICE = 'Template::Service';
39 $STASH = 'Template::Stash::XS';
40 $CONSTANTS = 'Template::Namespace::Constants';
42 @PRELOAD = ( $CONTEXT, $FILTERS, $ITERATOR, $PARSER,
43 $PLUGINS, $PROVIDER, $SERVICE, $STASH );
45 # the following is set at installation time by the Makefile.PL
49 #========================================================================
50 # --- CLASS METHODS ---
51 #========================================================================
53 #------------------------------------------------------------------------
54 # preload($module, $module, ...)
56 # Preloads all the standard TT modules that are likely to be used, along
57 # with any other passed as arguments.
58 #------------------------------------------------------------------------
63 foreach my $module (@PRELOAD, @_) {
64 $class->load($module) || return;
70 #------------------------------------------------------------------------
73 # Load a module via require(). Any occurences of '::' in the module name
74 # are be converted to '/' and '.pm' is appended. Returns 1 on success
75 # or undef on error. Use $class->error() to examine the error string.
76 #------------------------------------------------------------------------
79 my ($class, $module) = @_;
82 eval { require $module; };
83 return $@ ? $class->error("failed to load $module: $@") : 1;
87 #------------------------------------------------------------------------
90 # Instantiate a new parser object of the class whose name is denoted by
91 # the package variable $PARSER (default: Template::Parser). Returns
92 # a reference to a newly instantiated parser object or undef on error.
93 # The class error() method can be called without arguments to examine
94 # the error message generated by this failure.
95 #------------------------------------------------------------------------
99 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
102 return undef unless $class->load($PARSER);
103 return $PARSER->new($params)
104 || $class->error("failed to create parser: ", $PARSER->error);
108 #------------------------------------------------------------------------
111 # Instantiate a new template provider object (default: Template::Provider).
112 # Returns an object reference or undef on error, as above.
113 #------------------------------------------------------------------------
117 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
120 return undef unless $class->load($PROVIDER);
121 return $PROVIDER->new($params)
122 || $class->error("failed to create template provider: ",
127 #------------------------------------------------------------------------
130 # Instantiate a new plugins provider object (default: Template::Plugins).
131 # Returns an object reference or undef on error, as above.
132 #------------------------------------------------------------------------
136 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
139 return undef unless $class->load($PLUGINS);
140 return $PLUGINS->new($params)
141 || $class->error("failed to create plugin provider: ",
146 #------------------------------------------------------------------------
149 # Instantiate a new filters provider object (default: Template::Filters).
150 # Returns an object reference or undef on error, as above.
151 #------------------------------------------------------------------------
155 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
158 return undef unless $class->load($FILTERS);
159 return $FILTERS->new($params)
160 || $class->error("failed to create filter provider: ",
165 #------------------------------------------------------------------------
168 # Instantiate a new Template::Iterator object (default: Template::Iterator).
169 # Returns an object reference or undef on error, as above.
170 #------------------------------------------------------------------------
176 return undef unless $class->load($ITERATOR);
177 return $ITERATOR->new($list, @_)
178 || $class->error("failed to create iterator: ", $ITERATOR->error);
182 #------------------------------------------------------------------------
185 # Instantiate a new template variable stash object (default:
186 # Template::Stash). Returns object or undef, as above.
187 #------------------------------------------------------------------------
191 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
194 return undef unless $class->load($STASH);
195 return $STASH->new($params)
196 || $class->error("failed to create stash: ", $STASH->error);
200 #------------------------------------------------------------------------
203 # Instantiate a new template context object (default: Template::Context).
204 # Returns object or undef, as above.
205 #------------------------------------------------------------------------
209 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
212 return undef unless $class->load($CONTEXT);
213 return $CONTEXT->new($params)
214 || $class->error("failed to create context: ", $CONTEXT->error);
218 #------------------------------------------------------------------------
221 # Instantiate a new template context object (default: Template::Service).
222 # Returns object or undef, as above.
223 #------------------------------------------------------------------------
227 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
230 return undef unless $class->load($SERVICE);
231 return $SERVICE->new($params)
232 || $class->error("failed to create context: ", $SERVICE->error);
236 #------------------------------------------------------------------------
237 # constants(\%params)
239 # Instantiate a new namespace handler for compile time constant folding
240 # (default: Template::Namespace::Constants).
241 # Returns object or undef, as above.
242 #------------------------------------------------------------------------
246 my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
249 return undef unless $class->load($CONSTANTS);
250 return $CONSTANTS->new($params)
251 || $class->error("failed to create constants namespace: ",
256 #------------------------------------------------------------------------
259 # Returns the root installation directory appended with any local
260 # component directory passed as an argument.
261 #------------------------------------------------------------------------
264 my ($class, $dir) = @_;
266 || return $class->error("no installation directory");
268 $inst .= "/$dir" if $dir;
273 #========================================================================
274 # This should probably be moved somewhere else in the long term, but for
275 # now it ensures that Template::TieString is available even if the
276 # Template::Directive module hasn't been loaded, as is the case when
277 # using compiled templates and Template::Parser hasn't yet been loaded
279 #========================================================================
281 #------------------------------------------------------------------------
282 # simple package for tying $output variable to STDOUT, used by perl()
283 #------------------------------------------------------------------------
285 package Template::TieString;
288 my ($class, $textref) = @_;
289 bless $textref, $class;
293 $$self .= join('', @_);
304 Template::Config - Factory module for instantiating other TT2 modules
308 use Template::Config;
312 This module implements various methods for loading and instantiating
313 other modules that comprise the Template Toolkit. It provides a consistent
314 way to create toolkit components and allows custom modules to be used in
315 place of the regular ones.
317 Package variables such as C<$STASH>, C<$SERVICE>, C<$CONTEXT>, etc., contain
318 the default module/package name for each component (L<Template::Stash>,
319 L<Template::Service> and L<Template::Context>, respectively) and are used by
320 the various factory methods (L<stash()>, L<service()> and L<context()>) to
321 load the appropriate module. Changing these package variables will cause
322 subsequent calls to the relevant factory method to load and instantiate an
323 object from the new class.
325 =head1 PUBLIC METHODS
329 Load a module using Perl's L<require()>. Any occurences of 'C<::>' in the module
330 name are be converted to 'C</>', and 'C<.pm>' is appended. Returns 1 on success or
331 undef on error. Use C<$class-E<gt>error()> to examine the error string.
335 This method preloads all the other C<Template::*> modules that are likely to
336 be used. It is called automatically by the L<Template> module when running
337 under mod_perl (C<$ENV{MOD_PERL}> is set).
339 =head2 parser(\%config)
341 Instantiate a new parser object of the class whose name is denoted by
342 the package variable C<$PARSER> (default: L<Template::Parser>). Returns
343 a reference to a newly instantiated parser object or undef on error.
345 =head2 provider(\%config)
347 Instantiate a new template provider object (default: L<Template::Provider>).
348 Returns an object reference or undef on error, as above.
350 =head2 plugins(\%config)
352 Instantiate a new plugins provider object (default: L<Template::Plugins>).
353 Returns an object reference or undef on error, as above.
355 =head2 filters(\%config)
357 Instantiate a new filter provider object (default: L<Template::Filters>).
358 Returns an object reference or undef on error, as above.
362 Instantiate a new stash object (L<Template::Stash> or L<Template::Stash::XS>
363 depending on the default set at installation time) using the contents of the
364 optional hash array passed by parameter as initial variable definitions.
365 Returns an object reference or undef on error, as above.
367 =head2 context(\%config)
369 Instantiate a new template context object (default: L<Template::Context>).
370 Returns an object reference or undef on error, as above.
372 =head2 service(\%config)
374 Instantiate a new template service object (default: L<Template::Service>).
375 Returns an object reference or undef on error, as above.
377 =head2 iterator(\%config)
379 Instantiate a new template iterator object (default: L<Template::Iterator>).
380 Returns an object reference or undef on error, as above.
382 =head2 constants(\%config)
384 Instantiate a new namespace handler for compile time constant folding
385 (default: L<Template::Namespace::Constants>). Returns an object reference or
386 undef on error, as above.
390 Returns the root directory of the Template Toolkit installation under
391 which optional components are installed. Any relative directory specified
392 as an argument will be appended to the returned directory.
394 # e.g. returns '/usr/local/tt2'
395 my $ttroot = Template::Config->instdir()
396 || die "$Template::Config::ERROR\n";
398 # e.g. returns '/usr/local/tt2/templates'
399 my $template = Template::Config->instdir('templates')
400 || die "$Template::Config::ERROR\n";
402 Returns C<undef> and sets C<$Template::Config::ERROR> appropriately if the
403 optional components of the Template Toolkit have not been installed.
407 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
411 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
413 This module is free software; you can redistribute it and/or
414 modify it under the same terms as Perl itself.
424 # perl-indent-level: 4
425 # indent-tabs-mode: nil
428 # vim: expandtab shiftwidth=4: