in reply to Template Toolkit "Pre-Processor"

I don't know if Template-Toolkit offers such an option (I assume you have searched for one already, and haven't found one), but it shouldn't be very hard to do it by yourself.

From the syntax exmples in the docs it seems you only have to parse

[% PROCESS template var=val var=val ... %] [% INCLUDE template var=val var=val ... %]

Since there's no tag nesting, it can be done with regexes.

A first ad-hoc attempt, not tested:

my $re = qr{ \[%-? # start tag \s* (?:PROCESS|INCLUDE) \s+ (\S+) # template name .*? # any parameters you're not interested in -?%\] # end tag }xs;

Try if it matches all you need, if not refine it.

And yes, that's only an ad-hoc solution, and wo't scale very well.

(Update: template name is probably \S+, \w+ is too restrictive)

Replies are listed 'Best First'.
Re^2: Template Toolkit "Pre-Processor"
by pc88mxer (Vicar) on Feb 29, 2008 at 16:11 UTC
    To build on this solution, to properly handle passing parameters I think you need to replace

    [% INCLUDE file var1=val1 var2=val2 ... %]

    with:

    [% BLOCK _inline_file %] ...contents of file... [% END BLOCK %] [% INCLUDE _inline_file var1=val1 var2=val2 ... %]

    and the same for PROCESS directives. There can be complications if you use INCLUDE and PROCESS with BLOCK identifiers. In that case you'll have to determine if the template argument refers to a file or a BLOCK. Finally, if you include or process a template file twice you'll want to make sure you don't define the corresponding inlined block twice.

    btw, this would be an excellent question for the template toolkit list.