<p>在jinja2加载模板之前,似乎没有设置环境设置。</p>
<blockquote>
<p><strong>class jinja2.Environment([options])</strong></p>
<p>... Instances of this class may be modified if they are not shared and <strong>if no template was loaded so far.</strong> Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior.</p>
<p><a href="http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment" rel="noreferrer">http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment</a></p>
</blockquote>
<p>检查代码的顺序/结构,查看环境设置与模板的加载方式。</p>
<p>顺便说一下,jinja2的<a href="http://jinja.pocoo.org/docs/dev/templates/#whitespace-control" rel="noreferrer">whitespace control</a>确实可以按预期工作,而不需要复杂的环境和加载:</p>
<pre class="lang-python prettyprint-override"><code>import jinja2
template_string = '''<div>
{% if x == 3 %}
<small>{{ x }}</small>
{% endif %}
</div>
'''
# create templates
template1 = jinja2.Template(template_string)
template2 = jinja2.Template(template_string, trim_blocks=True)
# render with and without settings
print template1.render(x=3)
print '\n<!-- {} -->\n'.format('-' * 32)
print template2.render(x=3)
</code></pre>
<hr/>
<pre class="lang-html prettyprint-override"><code><div>
<small>3</small>
</div>
<!-- -------------------------------- -->
<div>
<small>3</small>
</div>
</code></pre>
<p>我没有使用jinja2,但扫描完文档后,加载顺序似乎有问题。</p>