Search code examples
perlhooktemplate-toolkit

Template Toolkit include hook?


Is there some type of a hook that will be executed when I include a template and in which I can change the path to the template?

e.g.

[% INCLUDE test %]

And I want to change the path to 'test' to be something like 'AE/test.tt' or 'FR/test.tt'.


The idea is to change the path to the template without changing the template in which the include is situated.


Solution

  • The normal way of doing this would be to use the INCLUDE_PATH parameter when constructing your Template object:

    my $tt = Template->new({INCLUDE_PATH => 'AE'});
    

    Building a new Template instance isn't any big deal, so there's no strong reason not to make a new one on the fly whenever you're about to process a template, but, if you're in a persistent environment and really need the performance, there's always the option of creating more than one:

    my %tt;
    $tt{ae} = Template->new({INCLUDE_PATH => 'AE'});
    $tt{fr} = Template->new({INCLUDE_PATH => 'FR'});
    # ... code ... code ... code ...
    $lang = 'ae';
    # ... code ... code ... code ...
    $tt{$lang}->process($template_name);