[Templates] Need context information within dynamic filter

Sergey Martynoff sergey@martynoff.info
Sat, 25 Nov 2006 22:41:44 +0300


	
> I have written a custom dynamic filter ; its goal being
internationalization of templates. Now I need that filter to find out about
its calling template (I don't ask for the line number in the template).
> I found out that filters get a Template::Context object as their first
parameter. A few dump later I managed to get the calling template filename
via the component entry of the stash. Here is my code :

>	sub myCustomFilter {
>	    my ($context, @arg) = @_;
>
>	    my $stash = $context->stash();
>	    my $component = $stash->get('component');
>	    my $template_name = $component->{'name'};

> I also need to find out the full path of the calling template. 
> How can I get it from the Template::Context object ?

You can examine $context->{LOAD_TEMPLATES} to find current component and
then get it's full path. This sample code works when called from template,
but not from BLOCK:

	my $stash = $context->stash();
	my $component = $stash->get('component');
	my ($provider) = grep { $_->{HEAD}[2] eq $component } @{
$context->{LOAD_TEMPLATES} };
	my $path = $provider->{HEAD}[1] if $provider;

Anyway, linking to file name seems to be a bad practice, because some
templates may have no file name at all - it could be hard-coded in perl code
or served from database.


-- 
Sergey Martynoff