A few days ago, I was asking about how to automatically include the template
name in start and end tags at the beginning and end of each INCLUDEd
template (original email is below). Since this is specifically for a Web
application, I want those to be start and end HTML comments. What I came up
with, in my module, was to grab &Template::Context::include and wrap its
return value in the appropriate data:
BEGIN {
use Template::Context;
no strict;
no warnings;
my $sub = *{$Template::Context::{include}}{CODE};
*Template::Context::include = sub {
my $tmpl = $_[1];
my $data = $sub->(@_);
$data = "<!-- START: $tmpl -->\n$data\n<!-- END: $tmpl -->";
return $data;
}
}
Frankly, I don't know a heck of a lot about the internals of Template
Toolkit, but this seemed to work just fine. Are there any pitfalls to this
approach?
Cheers,
Curtis "Ovid" Poe
--
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A
----- Original Message -----
From: "Curtis Poe" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 04, 2002 4:05 PM
Subject: [Templates] Dynamically altering static templates
> I first asked this question on Perlmonks
> (http://www.perlmonks.org/index.pl?node_id=156772) and was directed here.
> You might find some of the replies there helpful.
>
> ----
>
> 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
> (templates are served from directories). 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: [% component.name %] -->
> <table>
> <tr>
> <td>Some text</td>
> </tr>
> </table>
> <!-- END: [% component.name %] -->
>
> The problem, though, is that I have to hardcode those comments into each
> 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. From the docs:
>
> "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
> (Template::Context?) 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?
>
> Note that one of the suggestions was that PRE_INCLUDE and POST_INCLUDE
> directives would be nice...
>
> Cheers,
> Curtis "Ovid" Poe
> --
> "Ovid" on http://www.perlmonks.org/
> Someone asked me how to count to 10 in Perl:
> push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
> shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A
>
>
> _______________________________________________
> templates mailing list
> [EMAIL PROTECTED]
> http://www.template-toolkit.org/mailman/listinfo/templates
>