[Templates] modify config options

Josh Rosenbaum josh@infogears.com
Fri, 18 May 2007 11:27:37 -0600


Tim Keefer wrote:
> I would like to modify some of the template configuration options after 
> the TT object is created. For example, I'd like to change the WRAPPER 
> setting "midstream" without having to recreate the TT object. As far as 
> I can tell, the WRAPPER setting must be passed to the constructor. Is 
> there already a way to do this?
> 
> my $tt = Template->new({
> WRAPPER         => 'wrapper1.tt',
> INCLUDE_PATH    => '/mydir',       });
> 
> # maybe do something like this
> $tt->wrapper( 'new_wrapper.tt' );
> or
> $tt->set_wrapper( 'new_wrapper.tt' );


After you do Template->new, you could do:
$tt->service->{WRAPPER} = ['new_wrapper.tt'];

Although, I might be wary of doing that since that behavior could change. (Generally a bad idea to modify another classes parts.) I also have no clue if that would have any side-effects. ( I didn't look at the code that closely. It's in Template/Service.pm if you want to take a gander. Might need to check elsewhere as well, though.)

A couple of alternatives:
*) Use a code routine for include path and toggle the first search directory based on what you need to do. (Just keep the same wrapper name, but put the wrappers in different directories.
*) make your wrapper do the decision to use a different template. In essence, it'd just be a shell. For example:
[% IF foo %]
[% PROCESS new_wrapper.tt %]
[% ELSE %]
[% PROCESS wrapper_default.tt %]
[% END %]

-- Josh