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

  • Home
  • SEARCH
  • 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 3452548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:16:15+00:00 2026-05-18T09:16:15+00:00

Can one compile or revert a portion of the Jinja2 AST? For example, is

  • 0

Can one compile or revert a portion of the Jinja2 AST?

For example, is it possible to call a function or method from jinja2.environment or jinja2.compiler.generate or some equivalent on a list of nodes extracted from within a template?

For example, given a template y.html:

avant-tag
{% xyz %}
tag content {{ 3 + 5 }}
{% endxyz %}
apres-tag

and an extension y.py:

# -*- coding: utf-8 -*-
from jinja2 import nodes, Environment, FileSystemLoader
from jinja2.ext import Extension

class YExtension(Extension):
    tags = set(['y'])

    def __init__(self, environment):
        super(YExtension, self).__init__(environment)

    def parse(self, parser):
        tag = parser.stream.next()
        body = parser.parse_statements(['name:endy'], drop_needle=True)
        return nodes.Const("<!-- slurping: %s -->" % str(body))

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

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

Running python y.py results in the expected output of:

avant-tag
 <!-- slurping: [Output(nodes=[TemplateData(data=u'\n    tag-content '),
   Add(left=Const(value=3), right=Const(value=5)),
   TemplateData(data=u'\n ')])] -->
sous-tag

In the parse method, how can one either:

  1. compile body to unicode (i.e. tag-content 8); or, alternatively
  2. revert body to its original source (i.e. tag-content {{ 3 + 5 }}).

As a matter of background, this question relates to two prior questions:

  1. Jinja2 compile extension after includes; and
  2. Insert javascript at top of including file in Jinja 2

Thank you for reading.

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-18T09:16:16+00:00Added an answer on May 18, 2026 at 9:16 am

    Compiling to unicode is not yet possible in the parse() method since you don’t have the context available at that point. You can hack around it ofcourse but it would probably not be the best way to go.

    Note that the parse() step is normally only executed once on a html file, after that it will use the parsed bytecode to render the template. The results of the parse step can be rendered given an environment.

    You simply don’t have the context available there, and getting the context in there… quite difficult 😉

    To get the original source however… not much easier without hacking, but the hacking isn’t too bad 😉

    class YExtension(Extension):
        tags = set(['y'])
    
        def preprocess(self, source, name, filename=None):
            # insert some code here that replaces '{% xyz %}foo bar{% endxyz %}'
            # with something like: '{% xyz %}foo bar{% raw %}foo bar{% endraw %}{% endxyz %}'
            return source
    

    After that you can read the text as the value from the {% raw %} node. Be sure to trash it after that or it will show in your template.

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

Sidebar

Related Questions

Can one call a method from another model in a model in CodeIgniter? I
How can one call a ColdFusion function, passing in attribute values as arguments, inside
To compile a python file from shell, one can type python -O filename.py In
How can one compile a XeLaTeX tex document using latexmk on Mac OS X?
One can define a static array at compile time as follows: const std::size_t size
These two methods are practically the same, yet the first one can't compile. I
Can one of you explain why the following piece of code does not compile?
Can any one help me how to run this code... If I compile this
I would like to expose a function that can take an optional anonymous method
Can some one tell me what is the command to compile a simple GLUT

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.