pongo2 icon indicating copy to clipboard operation
pongo2 copied to clipboard

Access context from filter

Open globizcorp opened this issue 4 years ago • 1 comments

It would be a good feature if we could access the context from a filter and use some global variables here, like the selected language, using of preview mode, etc.

For example, consider the following email template:

<tr>
	<td><b>{{'mail_order_id'|lang:ln}}</b></td>
	<td>{{order.Id}}</td>
</tr>
<tr>
	<td><b>{{'mail_order_name'|lang:ln}}</b></td>
	<td>{{order.Name}}</td>
</tr>

Here we have a custom filter, which can translate the corresponding strings. But we have to pass the ln param to every filter, because the filter itself doesn't know the actual language, however it has been set in the context (context["ln"] = "en").

Another example is an embedded image: <img src="{{'spacer.gif'|images:preview}}"/> Here the filter replaces the src to a full URL (in preview mode) or to a cid:xxxx embed mark. Another issue here is that we need to store somehow which images we should include later, when sending the actual mail, so we can embed them. Now we have to rescan the template file, looking for src attribs and css backgrounds, and get the images from here, however the images filter could do that for us and store the image list on the context itself. Later we can easily grab the info from here.

Thanks for your great work!

globizcorp avatar Jun 22 '21 18:06 globizcorp

Support this. Without context the filter misses lots of state/context.

In my case I had to switch to function to use context but filter is semantically correct because it's about output.

My case: https://github.com/flosch/pongo2/issues/200#issuecomment-811024273

xc avatar Nov 11 '22 20:11 xc