You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the process of developing a custom Jinja2 extension that creates namespaces with dynamically evaluated names, I need to use the result of the evaluation of a template expression as argument to a Name node.
As far as I understand, the AST results from static evaluation, while the expression requires runtime evaluation (because expressions can contain variables whose value is not known at parse time).
The issue that I'm facing is that the Name node's first argument must be of string type, while the result of parsing the template expression is an AST node. And I don't readily understand how I can pass the latter as argument to the former. What would be the correct way to achieve this?
Follows a sample extension (not the actual, more complex extension that I'm working on) that captures the essence of what I'm trying to accomplish:
{% set my_name = 'my_namespace' %}
{% namespace my_name %}
{# A namespace named 'my_namespace' should now exist #}
Of course, I can easily parse the name expression and have the result of its evaluation displayed with an Output node. I can also create namespaces with predefined literal names:
from jinja2 import nodes
from jinja2.ext import Extension
class NamespaceExtension(Extension):
tags = {"namespace"}
def __init__(self, environment):
super().__init__(environment)
def parse(self, parser):
lineno = next(parser.stream).lineno
eval_context = nodes.EvalContext(self.environment)
name = parser.parse_expression()
##
## This obviously works as expected and outputs the result
## of evaluating `name` as an expression.
##
return nodes.Output([name]).set_lineno(lineno)
##
## This below also works but we need the namespace name to be
## evaluated dynamically as the result of the `name` expression.
##
# return nodes.Assign(
# nodes.Name('my_namespace', 'store'),
# nodes.Call(nodes.Name('namespace', 'load'), [], [], None, None)
# ).set_lineno(lineno)
namespace = NamespaceExtension
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello there!
In the process of developing a custom Jinja2 extension that creates namespaces with dynamically evaluated names, I need to use the result of the evaluation of a template expression as argument to a
Name
node.As far as I understand, the AST results from static evaluation, while the expression requires runtime evaluation (because expressions can contain variables whose value is not known at parse time).
The issue that I'm facing is that the
Name
node's first argument must be of string type, while the result of parsing the template expression is an AST node. And I don't readily understand how I can pass the latter as argument to the former. What would be the correct way to achieve this?Follows a sample extension (not the actual, more complex extension that I'm working on) that captures the essence of what I'm trying to accomplish:
Of course, I can easily parse the name expression and have the result of its evaluation displayed with an Output node. I can also create namespaces with predefined literal names:
Beta Was this translation helpful? Give feedback.
All reactions