Steve Steve - 2 years ago 84
Python Question

How do you parse and inject additional nodes in a Jinja extension?

I am attempting to adapt the Jinja2 WithExtension to produce a generic extension for wrapping a block (followed by some more complex ones).

My objective is to support the following in templates:

{% wrap template='wrapper.html.j2' ... %}
<img src="{{ url('image:thumbnail' ... }}">
{% endwrap %}

And for wrapper.html.j2 to look like something like:

some ifs and stuff
{{ content }}
more ifs and stuff

I believe my example is most of the way there, WithExtension appears to parse the block and then append the AST representation of some
{% assign .. %}
nodes into the context of the nodes it is parsing.

So I figured I want the same thing, those assignments, followed by an include block, which I'd expect to be able to access those variables when the AST is parsed, and to pass through the block that was wrapped as a variable

I have the following thus far:

class WrapExtension(Extension):
tags = set(['wrap'])

def parse(self, parser):
node = nodes.Scope(lineno=next(
assignments = []
while != 'block_end':
lineno =
if assignments:'comma')
target = parser.parse_assign_target()'assign')
expr = parser.parse_expression()
assignments.append(nodes.Assign(target, expr, lineno=lineno))
content = parser.parse_statements(('name:endwrap',), drop_needle=True)
assignments.append(nodes.Name('content', content))
assignments.append(nodes.Include(nodes.Template('wrapper.html.j2'), True, False))
node.body = assignments
return node

However, it falls over at my
line, I simply get
assert frame is None, 'no root frame allowed'
. I believe I need to pass AST to
rather than a template name, but I don't really know how to parse in additional nodes for the objective of getting AST rather than string output (i.e. renderings) – nor whether this is the right approach. Am I on the right lines, any ideas on how I should go about this?

Answer Source


class WrapExtension(jinja2.ext.Extension):
    tags = set(['wrap'])
    template = None

    def parse(self, parser):
        tag =
        lineno =
        args, kwargs = self.parse_args(parser)
        body = parser.parse_statements(['name:end{}'.format(tag)], drop_needle=True)

        return nodes.CallBlock(self.call_method('wrap', args, kwargs), [], [], body).set_lineno(lineno)

    def parse_args(self, parser):
        args = []
        kwargs = []
        require_comma = False

        while != 'block_end':
            if require_comma:

            if == 'name' and == 'assign':
                key =
                value = parser.parse_expression()
                kwargs.append(nodes.Keyword(key, value, lineno=value.lineno))
                if kwargs:
          'Invalid argument syntax for WrapExtension tag',

            require_comma = True

        return args, kwargs

    def wrap(self, context, caller, template=None, *args, **kwargs):
        return self.environment.get_template(template or self.template).render(dict(context, content=caller(), **kwargs))


{% wrap template='wrapper.html.j2' %}
    {% for i in range(3) %}
        im wrapped content {{ i }}<br>
    {% endfor %}
{% endwrap %}


Hello im wrapper
{{ content|safe }}

args/kwargs parsing get from here

Additionally, the above can be extended to support additional tags with a default template specified as the wrapper:


class ExampleExtension(WrapExtension):
    tags = set(['example'])
    template = 'example.html.j2'


{% example otherstuff=True, somethingelse=False %}
    {% for i in range(3) %}
        im wrapped content {{ i }}<br>
    {% endfor %}
{% endexample %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download