Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I am putting together some fairly complicated HTML templates with Template Toolkit, but the many-to-many database relationships do not map well to one-to-many directory structures. As a result, if our designer needs to work on one of the templates, he or she could be hard-pressed to find it. The cleanest solution that occurs to me is to allow the designers to "view source", and see each template delimited by start and end HTML comments that identify the template path:

<!-- START: [% %] --> <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor +="222222"> <tr> <td width="10"><img src="/images/1xshim.gif" width="10" height="1" +></td> <td valign="middle" class="textSubheader" width="100%">Some text</ +td> </tr> </table> <!-- END: [% %] -->

The problem, though, is that I have to hardcode those comments into each template ( is a built-in that lists the path and template). I could write a utility script that goes through and automatically inserts those into templates, if they are not found, but it would be cleaner to simply have Template Toolkit just insert that into the HTML for me. Designers might accidentally remove those comments, or forget to add them to new templates, etc.

My first thought was to use pre and post process configuration options.

my $template = Template->new({ PRE_PROCESS => 'start_tag.tmpl', POST_PROCESS => 'end_tag.tmpl', };

This did not work because I have templates that I INCLUDE into the main template. A careful reading of the docs makes it clear that this is not a feasible solution.

These values may be set to contain the name(s) of template files (relative to INCLUDE_PATH) which should be processed immediately before and/or after each template. These do not get added to templates processed into a document via directives such as INCLUDE, PROCESS, WRAPPER etc.

I've been reading through the docs and can't seem to find anything that seems suitable. My only other thought was to find the appropriate module to subclass and override the INCLUDE directive, but I'd prefer to write the aforementioned utility script than do this. Can anyone present a clean solution to this problem?


Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.