<p dir="auto">I'm Looking to create a similar custom extension to push any javascript code blocks to a designated area on the page or below footer.</p>
<p dir="auto">My version works using Python 3.6,Flask and Jinja 2.9. However I have a major<br>
issue that occurs after changing the line number or content within the blocks.<br>
The content will appear multiple times on render.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from jinja2 import nodes
from jinja2.ext import Extension

class JavascriptBuilderExtension(Extension):
    tags = set(['push'])

    def __init__(self, environment):
        super(JavascriptBuilderExtension, self).__init__(environment)
        self._myScope = {}
        environment.extend(
            pull = self._myScope
            )
    def parse(self, parser):
        &quot;&quot;&quot;Parse tokens &quot;&quot;&quot;
        tag = parser.stream.__next__()
        args = [parser.parse_expression(), nodes.Const(tag.lineno)]
        body = parser.parse_statements(['name:endpush'], drop_needle=True)
        callback = self.call_method('compiled', args)
        return nodes.CallBlock(callback,[], [], body).set_lineno(tag.lineno)

    def compiled(self,tagname,linenum,caller):
        tagname = &quot;{}_{}&quot;.format( tagname, linenum)
        self._myScope[tagname] = caller()
        return &quot;&lt;!-- moved {} from line {} --&gt;&quot;.format(tagname,linenum)"><pre class="notranslate"><code class="notranslate">from jinja2 import nodes
from jinja2.ext import Extension

class JavascriptBuilderExtension(Extension):
    tags = set(['push'])

    def __init__(self, environment):
        super(JavascriptBuilderExtension, self).__init__(environment)
        self._myScope = {}
        environment.extend(
            pull = self._myScope
            )
    def parse(self, parser):
        """Parse tokens """
        tag = parser.stream.__next__()
        args = [parser.parse_expression(), nodes.Const(tag.lineno)]
        body = parser.parse_statements(['name:endpush'], drop_needle=True)
        callback = self.call_method('compiled', args)
        return nodes.CallBlock(callback,[], [], body).set_lineno(tag.lineno)

    def compiled(self,tagname,linenum,caller):
        tagname = "{}_{}".format( tagname, linenum)
        self._myScope[tagname] = caller()
        return "&lt;!-- moved {} from line {} --&gt;".format(tagname,linenum)
</code></pre></div>
<p dir="auto">My template code looks like this</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
{% push 'js' %} X {% endpush %}
{% push 'html' %} Z {% endpush %}
{% push 'js' %} Y {% endpush %}
{{ pull }}
&lt;/body&gt; &lt;/html&gt;"><pre class="notranslate"><code class="notranslate">&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
{% push 'js' %} X {% endpush %}
{% push 'html' %} Z {% endpush %}
{% push 'js' %} Y {% endpush %}
{{ pull }}
&lt;/body&gt; &lt;/html&gt;
</code></pre></div>
<p dir="auto">My rendered output is below:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
name = hyper testing jinja
date = right now
&lt;!-- moved js_4 from line 4 --&gt;
&lt;!-- moved html_5 from line 5 --&gt;
&lt;!-- moved js_6 from line 6 --&gt;
{'js_4': ' X ', 'html_5': ' Z ', 'js_6': ' Y '}
&lt;/body&gt; &lt;/html&gt;"><pre class="notranslate"><code class="notranslate">&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
name = hyper testing jinja
date = right now
&lt;!-- moved js_4 from line 4 --&gt;
&lt;!-- moved html_5 from line 5 --&gt;
&lt;!-- moved js_6 from line 6 --&gt;
{'js_4': ' X ', 'html_5': ' Z ', 'js_6': ' Y '}
&lt;/body&gt; &lt;/html&gt;
</code></pre></div>
<p dir="auto"><strong>The Problem happens after I change the template block line number or content.</strong></p>
<p dir="auto"><strong>After changing content and line numbers</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
{% push 'js' %} ABC {% endpush %}

{% push 'html' %} Z {% endpush %}

{% push 'js' %} 123{% endpush %}
{{ pull }}
&lt;/body&gt; &lt;/html&gt;"><pre class="notranslate"><code class="notranslate">&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
{% push 'js' %} ABC {% endpush %}

{% push 'html' %} Z {% endpush %}

{% push 'js' %} 123{% endpush %}
{{ pull }}
&lt;/body&gt; &lt;/html&gt;
</code></pre></div>
<p dir="auto"><strong>Render changed blocks now has prior content</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
name = hyper testing jinja
date = right now
&lt;!-- moved js_4 from line 4 --&gt;

&lt;!-- moved html_7 from line 7 --&gt;

&lt;!-- moved js_9 from line 9 --&gt;
{'js_4': ' X ABC', 'html_5': ' Z ', 'js_6': ' Y ','js_9':'123','html_7':'Z'}
&lt;/body&gt; &lt;/html&gt;"><pre class="notranslate"><code class="notranslate">&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test template&lt;/h1&gt;
name = hyper testing jinja
date = right now
&lt;!-- moved js_4 from line 4 --&gt;

&lt;!-- moved html_7 from line 7 --&gt;

&lt;!-- moved js_9 from line 9 --&gt;
{'js_4': ' X ABC', 'html_5': ' Z ', 'js_6': ' Y ','js_9':'123','html_7':'Z'}
&lt;/body&gt; &lt;/html&gt;
</code></pre></div>
<p dir="auto">This issue causes duplicate content to be added into the response.</p>
<p dir="auto">Is there a way to call the extension on ever page request to re-parse the template for new changes? or Possible to not cache the enclosed extension blocks?</p>
<p dir="auto">I have already tried adding the code below to auto reload templates but does not help the issue.</p>
<p dir="auto"><code class="notranslate">app.jinja_env.auto_reload = True</code></p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Python version: 3.6</li>
<li>Flask version:0.10</li>
<li>Werkzeug version:0.12.2</li>
</ul>
<p dir="auto">Duplicate: Sorry<br>
I can't download the documentation ether pdf of zip file from flask website.<br>
Djoudi</p>