title: Compile subsection of Jinja2 AST
body: <p>Can one compile or revert a portion of the Jinja2 AST?</p>
  
  <p>For example, is it possible to call a function or method from <a href="https://github.com/mitsuhiko/jinja2/blob/master/jinja2/environment.py" rel="nofollow noreferrer"><code>jinja2.environment</code></a> or <a href="https://github.com/mitsuhiko/jinja2/blob/master/jinja2/compiler.py#L57" rel="nofollow noreferrer"><code>jinja2.compiler.generate</code></a> or some equivalent on a list of nodes extracted from within a template?</p>
  
  <p>For example, given a template <code>y.html</code>:</p>
  
  <pre><code>avant-tag
  {% xyz %}
  tag content {{ 3 + 5 }}
  {% endxyz %}
  apres-tag
  </code></pre>
  
  <p>and an extension <code>y.py</code>:</p>
  
  <pre><code># -*- 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("&lt;!-- slurping: %s --&gt;" % str(body))
  
  env = Environment(
      loader      = FileSystemLoader('.'),
      extensions  = [YExtension],
      )
  
  print env.get_template('x.html').render()
  </code></pre>
  
  <p>Running <code>python y.py</code> results in the expected output of:</p>
  
  <pre><code>avant-tag
   &lt;!-- slurping: [Output(nodes=[TemplateData(data=u'\n    tag-content '),
     Add(left=Const(value=3), right=Const(value=5)),
     TemplateData(data=u'\n ')])] --&gt;
  sous-tag
  </code></pre>
  
  <p>In the <code>parse</code> method, how can one either:</p>
  
  <ol>
  <li>compile <code>body</code> to unicode (i.e. <code>tag-content 8</code>); or, alternatively</li>
  <li>revert <code>body</code> to its original source (i.e. <code>tag-content {{ 3 + 5 }}</code>).</li>
  </ol>
  
  <p>As a matter of background, this question relates to two prior questions:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/4294837/jinja2-compile-extension-after-includes">Jinja2 compile extension after includes</a>; and</li>
  <li><a href="https://stackoverflow.com/questions/4292630/insert-javascript-at-top-of-including-file-in-jinja-2">Insert javascript at top of including file in Jinja 2</a></li>
  </ol>
  
  <p>Thank you for reading.</p>
  
  <p>Brian</p>