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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |