Sergey Martynoff wrote:
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 :
[...]
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;
Thank you for this information Sergey, it seems to work prety well.
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.
We have full control over the way TT2 are parsed and it appens that we never have hard-coded templates mixed with internationalization.


Reply via email to