将模板分成几部分并包括每个部分是坏的吗? [英] is dividing a template into parts and including each part bad?

查看:25
本文介绍了将模板分成几部分并包括每个部分是坏的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本模板,我想将其分成三部分:页眉、正文、页脚.然后我使用基本模板来包含三个子模板.但是,据我所知,这意味着我无法覆盖 {{ block }} 内容.那么使用包括一个坏主意吗?或者有没有办法覆盖包含模板中的块内容?

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

我的代码:

在 header.html 中

<头><script url="..."></script><link rel="..."/>{% 块 head_extension %}{% 结束块 %}<身体><标题><div class="headerstuff">

</标题>

然后在 body.html 文件中:

 

{% 块内容 %}Foo fum 我的内容{% 结束块 %}

footer.html:

 <页脚>{% 块页脚 %}版权所有 2015{% 结束块 %}</页脚>

base.html:

{% 包含 "header.html" %}{% 包含 "body.html" %}{% 包含 "footer.html" %}<!-- 以及不起作用的部分-->{% 块 head_extension %}<script src="unique_script"></script>{% 结束块 %}{% 块内容 %}我的独特内容{% 结束块 %}{% 块页脚 %}版权所有 2011{% 结束块 %}<!-- 结束损坏的 Django 模板尝试 -->

我做错了吗?模板文档似乎表明我正在尝试做的事情不起作用.这似乎是创建易于阅读的模板的最佳方式.将所有部分都放在一个大文件中是否更好?可以想象,页眉、正文和页脚元素比这个演示要大得多.但重点仍然存在.

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

提前致谢

解决方案

你的方法很好,但你这样做的顺序有误.首先html开始和结束标签不要拆分成不同的文件,最好放在base.html.

以下是如何遵循分手结构的示例:

base.html

<头><!-- 这里的一些东西应该包含在所有模板中,例如 js 或 css -->{% 阻止 extra_css %}<!-- 包含应用模板依赖的 css -->{% endblock extra_css %}{% 阻止 extra_js %}<!-- 包含应用模板依赖的 js -->{% endblock extra_js %}{% 阻止 extra_head %}<!-- 对于头脑中的其他任何东西-->{% endblock extra_head %}<身体>{% 块菜单 %}<!-- 这里的默认菜单-->{% 阻止 extra_menu %}<!-- 基于模板扩展菜单-->{% endblock extra_menu %}{% endblock 菜单 %}{% 块内容 %}<div>这很好</div>{% 端块含量 %}{% 包含 "footer.html" %}{% 块底部_js %}<!-- 如果您想在底部使用手动 js 脚本-->{% endblock bottom_js %}

现在 base.html 已经全部设置好了 让我们假设从另一个子模板你想覆盖 base.htmlcontent 你会做:

child.html

{% 扩展 "base.html" %}{% 块内容 %}<div>这真的很好</div>{% 端块含量 %}

所以当页面被加载时,你会看到 this is really good 而不是 this is good(在内容块中的 base.html 中定义),因为你只需覆盖它.

如果您希望 base.html 中的内容块内的任何内容也应该保留,那么您需要扩展该块而不是使用方法 {{ block.super }}

child.html

{% 扩展 "base.html" %}{% 块内容 %}{{ block.super }}<div>这真的很好</div>{% 端块含量 %}

现在您将看到 this is goodthis is really good.希望这会澄清您的概念并引导您做好.

I have a base template that I'd like to split up into three parts: header, body, footer. Then I use the base template to include the three sub-templates. However, from what I've seen, this means I cannot override {{ block }} content. Is using includes a bad idea then? Or is there a way to override block content in an included template?

I know that you can send static context variables to the included segment, but it needs to be more dynamic.

My code:

In header.html

<html>
    <head>
        <script url="..."></script>
        <link rel="..." />
        {% block head_extension %}

        {% endblock %}
    </head>
    <body>
        <header>
            <div class="headerstuff">
            </div>
        </header>

Then in the body.html file:

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

Am I doing something wrong? The templating documentation seemed to indicate that what I am trying to do doesn't work. It seems like this would be the best way to create easy-to-read templates. Is it better just to have all parts in one large file? As you can imagine, the header, body, and footer elements are much larger than this demonstration. But the point remains.

I am hoping there is a way to do what I'm thinking of that I'm not aware of.

Thanks in advance

解决方案

Your approach is fine but you are doing this in wrong order. First of all the html starting <html> and ending tag </html> should not be split into different files, it is good to have it in base.html.

Below is an example of how to follow the breakup structure:

base.html

<html>
    <head>
        <!-- Some stuff here which should be included in all templates for example js or css -->
        {% block extra_css %}
            <!-- to included app-template dependent css -->
        {% endblock extra_css %}

        {% block extra_js %}
            <!-- to included app-template dependent js -->
        {% endblock extra_js %}

        {% block extra_head %}
            <!-- for anything else inside head -->
        {% endblock extra_head %}

    </head>
    <body>
        {% block menu %}
            <!-- Default menu here -->
            {% block extra_menu %}
                <!-- extend menu based on template -->
            {% endblock extra_menu %}
        {% endblock menu %}

        {% block content %}
            <div>This is good</div>
        {% endblock content %}

        {% include "footer.html" %}

        {% block bottom_js %}
            <!-- if you want to have manual js scripts at bottom -->
        {% endblock bottom_js %}
    </body>
</html>

Now base.html is all setup now lets suppose from another child template you want to override the base.html block content you will do:

child.html

{% extends "base.html" %}

{% block content %}
    <div>This is really good</div>
{% endblock content %}

So when the page will be loaded you will see this is really good instead of this is good (which was defined in base.html inside content block) because you just override it.

If you want that whatever inside the content block in base.html should also be preserved then you need to extend the block rather than overriding it completely by using method {{ block.super }}

child.html

{% extends "base.html" %}

{% block content %}
    {{ block.super }}
    <div>This is really good</div>
{% endblock content %}

Now you will see both this is good and this is really good. Hope this will clarify your concept and will lead you good.

这篇关于将模板分成几部分并包括每个部分是坏的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆