假定有一个Swig模板base.html
,该模板的变量为page
:
<!-- base.html -->
<div {% if page == 'home' %}class="active"{% endif %}>
使用{% include 'components/content.html'}
指令(如
<!-- base.html -->
{% include 'components/content.html'}
page
变量在根目录和包含的模板中都可用吗?
<!-- components/content.html -->
<ul>
{% if page == 'home' %}<li class="home"></li>
{% else %}
<li class="other"></li>
{% endif %}
<ul>
在我的测试中,似乎page
变量在所包含的模板中为undefined
,因此无法对其进行评估,因此{% if page == 'home' %}
将始终被评估为false
。