Hello.
A colleague of mine asked if a template knows which template called
it (if it was indeed called by another template). He was hoping to
do some conditional processing in a component based on which
template/component called it. So I wrote this patch to
Template::Context to add two fields to the special variable
'component'. The first is 'caller', which holds the name of the
template that called the current template (or undef if it was not
called by another template). The second is 'callers', which is a
list of all the templates which have been called on the road to
calling the current template. So if outer.tt2 PROCESSes/INCLUDEs
middle.tt2 and middle.tt2 PROCESSes/INCLUDEs inner.tt2, the values
for caller and callers would be (if outer.tt2 was the template
handed to a $tt->process() call):
In outer.tt2:
[% component.caller %] # undef
[% component.callers %] # undef
In middle.tt2:
[% component.caller %] # outer.tt2
[% component.callers %] # [ outer.tt2 ]
In inner.tt2:
[% component.caller %] # middle.tt2
[% component.callers %] # [ outer.tt2, middle.tt2 ]
The patch, against TT2.12a, appears below for your consideration.
All current tests appear to pass with the patch applied. I'd be
happy to write a patch for the docs if this patch is approved.
Thanks,
Dave
/L\_/E\_/A\_/R\_/N\_/T\_/E\_/A\_/C\_/H\_/L\_/E\_/A\_/R\_/N\
Dave Cash Power to the People!
Frolicking in Fields of Garlic Right On-Line!
[EMAIL PROTECTED] Dig it all.
diff -ru Context.pm.orig Context.pm
--- Context.pm.orig Tue Jan 13 10:19:10 2004
+++ Context.pm Sun Jan 18 20:35:44 2004
@@ -326,6 +326,11 @@
my $element = ref $compiled eq 'CODE'
? { (name => (ref $name ? '' : $name), modtime => time()) }
: $compiled;
+ if (UNIVERSAL::isa($component, 'Template::Document')) {
+ $element->{ caller } = $component->{ name };
+ $element->{ callers } = $component->{ callers } || [];
+ push(@{$element->{ callers }}, $element->{ caller });
+ }
$stash->set('component', $element);
unless ($localize) {
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates