A few days ago, I was asking about how to make Template Toolkit automatically insert identifying start and end comments around each INCLUDEd template. Here's what I finally came up with.

BEGIN { use Template::Context; no strict; no warnings; my $sub = *{$Template::Context::{include}}{CODE}; *Template::Context::include = sub { _wrapper( $sub, @_ ); }; $sub = *{$Template::Context::{process}}{CODE}; *Template::Context::process = sub { _wrapper( $sub, @_ ); }; } sub _wrapper { my $sub = shift; my $tmpl; if ( ! ref $_[1] ) { # [% INCLUDE ... %] $tmpl = $_[1]; } else { # $template->process(...) $tmpl = $_[1]->{name}; } my $data = $sub->(@_); $data = "<!-- START: $tmpl -->\n$data\n<!-- END: $tmpl -->"; return $data; }

Note that this is in an entirely separate module and leaves Template::Context.pm intact.

I need to add HTML::Entities to handle problems with funny characters in the template names and probably should strip the &process override and use use the PRE_PROCESS and POST_PROCESS directives. However, rather than go much further, I am wondering if anyone familiar with TT sees any pitfalls here? I'm reading through the code and docs and don't see anything wrong, but the code is very complex and I know I could easily have missed something.

Cheers,
Ovid

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


In reply to Overriding Template Toolkit Internals by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.