【问题标题】:Is there a way in jinja to display a block in a specific page?jinja 有没有办法在特定页面中显示块?
【发布时间】:2021-10-26 12:42:42
【问题描述】:

我有一个带有<footer> 的layout.html 页面,我想在几乎所有使用{% extends "layout.html %} 的页面中显示它。只有在用户个人资料页面中,页脚才会有所不同。我该怎么做?

【问题讨论】:

    标签: python flask jinja2


    【解决方案1】:

    您可以在布局页面中实现页脚块并相应地覆盖它:

    {# 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>
    

    对于不需要页脚的任何页面,只需忽略该块

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 2022-01-12
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多