[Templates] Need context information within dynamic filter
Olivier Salaün - CRU
olivier.salaun@cru.fr
Mon, 27 Nov 2006 10:04:40 +0100
This is a multi-part message in MIME format.
--------------020804090102040006030007
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
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.
--------------020804090102040006030007
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Sergey Martynoff wrote:
<blockquote cite="mid20061125193414.11125264357@mail.m9com.ru"
type="cite">
<blockquote type="cite">
<pre wrap="">I have written a custom dynamic filter ; its goal being
</pre>
</blockquote>
<pre wrap=""><!---->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).
</pre>
<blockquote type="cite">
<pre wrap="">I found out that filters get a Template::Context object as their first
</pre>
</blockquote>
<pre wrap=""><!---->parameter. A few dump later I managed to get the calling template filename
via the component entry of the stash. Here is my code :
</pre>
[...]
<blockquote type="cite">
<pre wrap="">I also need to find out the full path of the calling template.
How can I get it from the Template::Context object ?
</pre>
</blockquote>
<pre wrap=""><!---->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;
</pre>
</blockquote>
Thank you for this information Sergey, it seems to work prety well.<br>
<blockquote cite="mid20061125193414.11125264357@mail.m9com.ru"
type="cite">
<pre wrap="">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.
</pre>
</blockquote>
We have full control over the way TT2 are parsed and it appens that we
never have hard-coded templates mixed with internationalization.<br>
<br>
<br>
</body>
</html>
--------------020804090102040006030007--