【问题标题】:flask template problem with included blocks包含块的烧瓶模板问题
【发布时间】:2021-05-13 13:48:03
【问题描述】:

您好,我在模板中显示块时遇到了一些问题。 我有 5 个文件。 3 个包含在 base.html 中,还有一个从它扩展而来。

问题是页眉和页脚文件中称为元和页脚的块。 它们在我的 index.html 中只是空的。

只有在 base.html 文件中直接声明的内容块才有效。 navigation.html 工作正常,但没有声明任何块。

在 base.html 文件模板中包含带有块的文件有问题还是我错过了什么?

header.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {% block meta %}
    {% endblock %}
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('index') }}">Brand</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="#navbarSupportedContent">
                {% include 'navigation.html' %}
            </div>
        </div>
    </nav>

页脚.html

</body>
<footer>
    {% block footer %}
    {% endblock %}
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8" crossorigin="anonymous"></script>
</footer>
</html>

base.html

{% include 'header.html' %}
        {% block content %}
        {% endblock %}
{% include 'footer.html' %}

index.html

{% extends 'base.html' %}
{% block meta %}
    <title>Titel for index page</title>
{% endblock %}
{% block title %}Willkommen bei Brand{% endblock %}
{% block content %}
    <p>This block works</p>
{% endblock %}

导航.html

<ul class="navbar-nav ms-auto">
    <li class="nav-item">
        <a class="nav-link {{ active_home }}" aria-current="page" href="{{ url_for('index') }}">Home</a>
    </li>
    <li class="nav-item">
        <a class="nav-link {{ active_contact }}" aria-current="page" href="{{ url_for('contact') }}">Kontakt</a>
    </li>
    <li class="nav-item">
        <a class="nav-link {{ active_impress }}" aria-current="page" href="{{ url_for('index') }}">Impressum</a>
    </li>
</ul>

【问题讨论】:

    标签: python html flask jinja2


    【解决方案1】:

    在 base.html 文件模板中包含带有块的文件是否有问题或者我错过了什么?

    块在继承链之外是不可见的。如果你用required标记一个块,你会看到错误:

    {% block meta required %}
    
    "Required block 'meta' not found"
    

    include'd 模板呈现为单独的继承链,因此它不会看到 index.html 中的块。

    include 标记对于包含模板并将该文件的渲染内容返回到当前命名空间很有用 https://jinja.palletsprojects.com/en/latest/templates/#include

    您不应期望将您的块传递给include'd 模板,并相应地设计模板继承。例如,见base template in jinja documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2020-06-25
      • 1970-01-01
      相关资源
      最近更新 更多