Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3439440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:18:38+00:00 2026-05-18T08:18:38+00:00

In Jinja2, is it possible to have a Node from the AST render after

  • 0

In Jinja2, is it possible to have a Node from the AST render after all include statements have completed?

This is a key piece of a solution to a bigger puzzle.

Example code:

x.py

from jinja2 import nodes, Environment, FileSystemLoader
from jinja2.ext import Extension

class XExtension(Extension):
    tags = set(['x', 'get_x'])

    def __init__(self, environment):
        super(XExtension, self).__init__(environment)
        environment.extend(
            x = 0,
        )

    def parse(self, parser):
        tag = parser.stream.next()
        return getattr(self, "_%s" % str(tag))(parser, tag)

   def _get_x(self, parser, tag):
        """ Return the output """
        return nodes.Const(self.environment.x)

   def _x(self, parser, tag):
        """ Add an x """
        self.environment.x += 1
        return nodes.Const('<!-- Adding an X -->')

env = Environment(
    loader      = FileSystemLoader('.'),
    extensions  = [XExtension],
    )

template = env.get_template('x.html')
print template.render()

x.html

Xx {% x %} Xx {% x %}
{% include "y.html" %}
Xs xes: {% get_x %}

y.html

Yx {% x %}
Ys xes: {% get_x %}

The output is

Xx <!-- Adding an X --> Xx <!-- Adding an X -->
Yx <!-- Adding an X -->
Ys xes:3
Xs xes 2

How may it be possible to have Xs xes count the X’s in y.html, which is the behaviour I desire and expect?

In other words, is there a way to delay the parsing or flattening to text returned from the _get_x() in x.html?

Thank you very much for reading.

Kind regards,

Brian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:18:39+00:00Added an answer on May 18, 2026 at 8:18 am

    Jinja2 does streaming of template data. The template is evaluated instruction for instruction into a stream of smaller strings that gets concatenated into an actual unicode string by the render() method. However you can also get hold of the stream by calling into generate() instead of render().

    With some in-band signalling and postprocessing of the stream one might probably be able to implement something like that, but it’s against the way of how Jinja2 was designed so it might break horribly and is totally unsupported.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Jinja2 , I have a base template like this: <title>{% block title %}{%
Let's say I have some Jinja2 template with several blocks in it: {% block
Jinja2 automatically escapes all HTML tags, but I want to not escape some tags
I'am new to the Jinja2 template engine. Is there something like the view-helpers from
I have a custom tag in Jinja2 that I want to output something only
Converting from Django, I'm used to doing something like this: {% if not var1
I'd like to use Jinja2 with a Django project. I'm using this template loader
So, I have a django project that is using jinja2 rendering, and I also
I have installed virtualenv along with flask, werkzeug, jinja2 and SQLAlchemy. I was following
Is that possible to define a function without referencing to self this way? def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.