On Tuesday 11 December 2007, Perrin Harkins wrote:
> On Dec 11, 2007 11:20 AM, Paul Seamons <[EMAIL PROTECTED]> wrote:
> > >From time to time I see people requesting allowing finding templates
> > > relative
> >
> > to the current template. I just responded to an email this morning with
> > how to do so in Template::Alloy. I would propose having it as an option
> > in Template::Toolkit.
>
> It's already pretty easy to do with the coderef capability of INCLUDE_PATH.
If by easy you mean passing the current template object as a weakened
reference to the closure, attempting to find the current path, manipulating
the current path and adding it to INCLUDE_PATH, then you are right - it is
easy.
Here is a sample hack that extremely error prone. I'd love to see a more
simple version:
use Template;
use POSIX qw(tmpnam);
use File::Path qw(rmtree mkpath);
my $dir = tmpnam;
mkpath($dir);
mkpath("$dir/dir2");
END { rmtree $dir };
open(my $fh, ">", "$dir/dir2/foo.tt");
print $fh "I am foo.tt";
open $fh, ">", "$dir/dir2/bar.tt";
print $fh 'I am bar.tt
------------
[% PROCESS "foo.tt" %]
';
close $fh;
my @paths;
my $t = Template->new(INCLUDE_PATH => [EMAIL PROTECTED]);
my $t_copy = $t;
push @paths, sub {
# use t_copy to figure out what template we are currently in
my @dirs = ($dir);
use CGI::Ex::Dump qw(debug);
my $file = $t_copy->context->{LOAD_TEMPLATES}[0]{HEAD}->[1] || '';
unshift @dirs, $1 if $file =~ m|(.+)/[^/]+$|;
return [EMAIL PROTECTED];
};
use Scalar::Util qw(weaken);
weaken $t_copy; # kill our circular ref
$t->process("dir2/bar.tt") || die $t->error;
Notice the nice dive into the internals of the object - that we hope will
always be set. Notice having to configure with an ref to array that is later
populated with a reference to the tt object. This is doable because it is
perl. I do not think that it would be construed as easy by many.
I still think having ADD_LOCAL_PATH would be a good thing. I'm hardly hurting
if TT doesn't add it -- but I think there are many TT users that could
benefit from having it.
Paul
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates