jinja 有没有办法在特定页面中显示块?
Is there a way in jinja to display a block in a specific page?
我有一个带有 <footer>
的 layout.html
页面,我想在几乎所有使用 {% extends "layout.html %}
的页面中显示它。仅在用户个人资料页面中,页脚会有所不同。我该怎么做?
您可以在布局页面中实现页脚块并相应地覆盖它:
{# layout.html #}
...
<div class="container">
...
{% block app_content %}{% endblock %}
{# block exists for all child templates #}
{% block footer %}{% endblock %}
</div>
...
{# profile.html #}
{% extends "layout.html" %}
{# the footer will automatically be placed inside container #}
{# you can override app_content for the body #}
{% block app_content %}...{% endblock %}
{% block footer %}{% include "profile_footer.html" %}{% endblock %}
{# footer.html #}
{# won't be included in profile.html #}
<footer>my footer</footer>
{# profile_footer.html #}
{# will be included in profile.html #}
<footer>profile footer</footer>
对于任何您不想要页脚的页面,只需省略块
我有一个带有 <footer>
的 layout.html
页面,我想在几乎所有使用 {% extends "layout.html %}
的页面中显示它。仅在用户个人资料页面中,页脚会有所不同。我该怎么做?
您可以在布局页面中实现页脚块并相应地覆盖它:
{# layout.html #}
...
<div class="container">
...
{% block app_content %}{% endblock %}
{# block exists for all child templates #}
{% block footer %}{% endblock %}
</div>
...
{# profile.html #}
{% extends "layout.html" %}
{# the footer will automatically be placed inside container #}
{# you can override app_content for the body #}
{% block app_content %}...{% endblock %}
{% block footer %}{% include "profile_footer.html" %}{% endblock %}
{# footer.html #}
{# won't be included in profile.html #}
<footer>my footer</footer>
{# profile_footer.html #}
{# will be included in profile.html #}
<footer>profile footer</footer>
对于任何您不想要页脚的页面,只需省略块