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.

Adding frame variables from custom tag

See original GitHub issue

I’m trying to make an extension which adds support for the trans tag. Jinja trans syntax requires you to bind template expressions to new variables via keyword arguments, like:

<p>{% trans user=user.username %}Hello {{ user }}!{% endtrans %}</p>

Now, I’m able to iterate over the arguments to my tag and add them to the context like this:

    this.run = function(context, args, body) {
        for (key in args) {
            context.ctx[key] = args[key];
        }
        return gettext(body()); # gettext() is defined somewhere else.
 }

But it seems I’m getting the global context and polluting this, which leaves my new variables available outside my trans tag (i.e. the user variable will be available everywhere afterwards).

How do I extend only the “local” context used by my tag? It seems the for and (in some cases) set tags do this and I tried looking through the source for this, but I’m unfortunately lost.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
KSDaemoncommented, Jan 3, 2016

Hi all, guys! Just faced the same kind of problem. I’m creating format.js internationalization helper for nunjucks templater. And one last thing i have to implement — is a custom ‘intl’ tag, that overrides global intl locale options provided. For ex., you can render whole template, using a ‘en’ locale, but have one block with ‘fr’ locale. Unfortunately, docs about hijacking nunjucks extensions api is very poor, but i’ve got somehow through sources, and have come to this.

What about adding access to frame stack through some method in ctx, like lookup? For example:

// inside run function of custom extension:
var frame = context.addFrame();
frame.set('my-scoped-var', 'scoped-value');

return content();

Or just allow raw access to frames stack, so developer can use push/pop and all other frame methods.

var frame = context.frames.push();
0reactions
djx314commented, Jan 14, 2017

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create data.frame with variable as tag - Stack Overflow
frame (colname=1:11) by providing variable x="colname" , I've tried data.frame(as.formula("x=1:11")) but id does not work, what is tag in data ...
Read more >
Chapter 8 Custom Tags in JSP Pages (The Java EE 5 Tutorial)
Declaring Tag Variables in Tag Files. Tag attributes are used to customize tag behavior much as parameters are used to customize the behavior...
Read more >
Custom tag variables - Adobe Support
A custom tag can set a Caller variable to provide information to the caller. Set the Caller variable as follows: ...
Read more >
[Guide] ElvUI Custom Tag Guide - Tukui
Custom Texts - This will allow you to create new element to add custom text to your frame. Element Selection - You can...
Read more >
<iframe>: The Inline Frame element - HTML - MDN Web Docs
The <iframe> HTML element represents a nested browsing context, embedding another HTML page into the current one. Try it. HTML Demo: <iframe>.
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