[Templates] Current template directory

Dennis C. De Mars demars@fractaldomains.com
Sun, 18 Feb 2007 17:00:01 -0800


It would be nice to have the path to the current directory of the  
template being processed (from the root of the template project to  
the current directory) defined in the "template" hash. Currently, the  
only way I can find to determine this information is to extract it  
from template.name, which works but is a little messy. So, it would  
be convenient if there were a field like "template.currdir" that  
would contain this information.

I am trying to work with relative paths as much as possible and for  
the most part this is working out, but I ran into a problem when I  
wanted to generate a bunch of .html files for a gallery from data  
defined in the gallery index page. I use the redirect filter for  
this, but redirect expects file paths to be specified relative to the  
root of the destination hierarchy. I want the files to be generated  
in the same folder as the gallery index; and, I am going to have  
several of these gallery folders in several different places, and I  
don't want to hard code the path from the root to the gallery folder  
for each folder.

So, I ended  up doing something like this:

	[% thisfile = template.name %]
	[%   PERL %]
	      use File::Basename;
	      (my $name, my $path, my $suffix) = fileparse($stash->get 
('thisfile'));
	      $stash->set(thisdir => $path);
	 [%  END %]
	 [%  FILTER redirect("$thisdir/${picture.image}.html");
		 INCLUDE page/gallery_image picture = picture;
	   END; %]

(Unimportant details: In this, page/gallery_image is  a template that  
expands into an entire gallery page, and picture is a hash with  
information about the image being displayed.)

And of course, what I would like to do is:

	 [%  FILTER redirect("$template.currdir/${picture.image}.html");
		 INCLUDE page/gallery_image picture = picture;
	   END; %]

Of course, I can define a macro or other template entity to hide the  
dirty work. The only reason I bring this up is that I searched the  
entire archive of this mailing list, and although I didn't see any  
methods to determine current template directory, I saw several  
requests on how to determine it, and people had various reasons for  
wanting to know this. So, it seems like providing it would make life  
a little bit easier for some users even if it is somewhat redundant.

- Dennis D.