将模板分为几个部分并包括每个部分是否不好?

我有一个基本模板,我想分为三个部分:页眉,正文,页脚。然后,我使用基本模板包括三个子模板。但是,从我所看到的情况来看,这意味着我无法覆盖{{block}}内容。那么使用包含一个坏主意吗?还是有一种方法可以覆盖包含的模板中的块内容?


我知道您可以将静态上下文变量发送到所包含的段,但是它需要更加动态。


我的代码:


在header.html中


<html>

    <head>

        <script url="..."></script>

        <link rel="..." />

        {% block head_extension %}


        {% endblock %}

    </head>

    <body>

        <header>

            <div class="headerstuff">

            </div>

        </header>

然后在body.html文件中:


        <div class="container">

            {% block content %}

                Foo fum my content    

            {% endblock %}

        </div>

footer.html:


        <footer>

            {% block footer %}

                Copyright 2015

            {% endblock %}

        </footer>

    </body>

</html>

base.html:


{% include "header.html" %}

{% include "body.html" %}

{% include "footer.html" %}

<!-- and the part that doesn't work -->

{% block head_extension %}

    <script src="unique_script"></script>

{% endblock %}

{% block content %}

    My unique content

{% endblock %}

{% block footer %}

    Copyright 2011

{% endblock %}

<!-- end broken django templating try -->

难道我做错了什么?模板文档似乎表明我尝试执行的操作不起作用。看来这将是创建易于阅读的模板的最佳方法。将所有部分都放在一个大文件中会更好吗?您可以想象,页眉,正文和页脚元素比此示例要大得多。但是重点仍然存在。


我希望有一种方法可以做我不知道的事情。


qq_遁去的一_1
浏览 235回答 1
1回答

Smart猫小萌

您的方法很好,但是您执行的顺序错误。首先,不应将html的开始<html>和结束标记</html>拆分为不同的文件,最好将其放入base.html。以下是如何遵循分手结构的示例:base.html<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Some stuff here which should be included in all templates for example js or css -->&nbsp; &nbsp; &nbsp; &nbsp; {% block extra_css %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- to included app-template dependent css -->&nbsp; &nbsp; &nbsp; &nbsp; {% endblock extra_css %}&nbsp; &nbsp; &nbsp; &nbsp; {% block extra_js %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- to included app-template dependent js -->&nbsp; &nbsp; &nbsp; &nbsp; {% endblock extra_js %}&nbsp; &nbsp; &nbsp; &nbsp; {% block extra_head %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- for anything else inside head -->&nbsp; &nbsp; &nbsp; &nbsp; {% endblock extra_head %}&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; {% block menu %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Default menu here -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% block extra_menu %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- extend menu based on template -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endblock extra_menu %}&nbsp; &nbsp; &nbsp; &nbsp; {% endblock menu %}&nbsp; &nbsp; &nbsp; &nbsp; {% block content %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>This is good</div>&nbsp; &nbsp; &nbsp; &nbsp; {% endblock content %}&nbsp; &nbsp; &nbsp; &nbsp; {% include "footer.html" %}&nbsp; &nbsp; &nbsp; &nbsp; {% block bottom_js %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- if you want to have manual js scripts at bottom -->&nbsp; &nbsp; &nbsp; &nbsp; {% endblock bottom_js %}&nbsp; &nbsp; </body></html>现在base.html是所有设置,现在让我们假设您想覆盖另一个要覆盖的base.html块的子模板content:child.html{% extends "base.html" %}{% block content %}&nbsp; &nbsp; <div>This is really good</div>{% endblock content %}因此,在加载页面时,您会看到this is really good而不是this is good(它在content块内的base.html中定义),因为您只是覆盖了它。如果您希望base.html保留内容块中的所有内容,则需要扩展该块,而不是通过使用方法{{block.super}}来完全覆盖它child.html{% extends "base.html" %}{% block content %}&nbsp; &nbsp; {{ block.super }}&nbsp; &nbsp; <div>This is really good</div>{% endblock content %}现在,您将同时看到this is good和this is really good。希望这可以澄清您的概念并为您带来好处。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python