question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access to a the templating context object in filter functions

See original GitHub issue

It would be quite useful to have access to the render context object inside filter functions; especially for localisation this could be very useful (at the very least for webmaker).

A use case:

we have a batch of key/strings that include templating vars:

"welcome": "welcome to the site, {{username}}"
"about": "<a href='{{lang}}/about'>click here</a>"
...

these are added (server-side) to our own template files, such as an index.html:

<html>
...
<p>{{ getText("welcome") }}</p>
...
<footer>{{ getText("about") }}</footer>
...
</html>

We render these the usual way, using a res.render("index.html", {...}), but this only gets us half-way there - the rendered data will look like:

<html>
...
<p>welcome to the site, {{username}}</p>
...
<footer><a href='{{lang}}/about'>click here</a></footer>
...
</html>

In order to instantiate these “mini-templates”, we thought of using a custom filter:

env.addFilter("finalize", function(input) {
    var tmpl = new nunjucks.Template(input),
        output = tmpl.render();
    return output;
});

with updated main template:

<html>
...
<p>{{ getText("welcome") | finalize }}</p>
...
<footer>{{ getText("about") | finalize }}</footer>
...
</html>

Unfortunately, the filter functions don’t have access to the same context object, so even though the original render("index.html") has all the variables we need for correct instantiation, the filter function(s) do not, leading to unknown vars simply getting stripped, and vars that were known in res.locals being unknown inside the filter function.

If the filter functions can be made context-aware, so that render calls inside the filter function have the same context object as the “owning” render call, that would be fantastic.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jlongstercommented, Jul 23, 2013

You need to use this, which points to the current Context object. Since you want to get just the raw object, you should use the getVariables function.

nunjucksEnv.addFilter("filter", function(input) {
    var tmpl = new nunjucks.Template(input);
    return tmpl.render(this.getVariables());
});
0reactions
alicodingcommented, Jul 22, 2013

So I’m not sure if I understand this right, but how am I going to set up the filter now after the change?

nunjucksEnv.addFilter("filter", function(input) {
    var tmpl = new nunjucks.Template(input);
    return tmpl.render(); // <-- ... not sure if this still works, is res.locals still active here?
});

So the above code when I put that filter in say {{ gettext("keyname") | filter }} the value that is being return it’s still not rendering, so I’m not sure if I’m missing something here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template Designer Documentation - Jinja
Template variables are defined by the context dictionary passed to the template. You can mess around with the variables in templates provided they...
Read more >
Accessing variable context name in template - django
I would suggest adding a method to your model to access the second query: class Tblsummary(models.Model): .
Read more >
nunjucks templating docs
Filters are essentially functions that can be applied to variables. ... or it can contain a compiled Template object that has been added...
Read more >
Resolver mapping template context reference - AWS AppSync
AWS AppSync defines a set of variables and functions for working with resolver mapping templates. This makes logical operations on data easier with...
Read more >
filter - Twig - The flexible, fast, and secure PHP template engine
The filter filter filters elements of a sequence or a mapping using an arrow function. ... Note that the arrow function has access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found