将模板分成几部分,包括每个部分不好? [英] is dividing a template into parts and including each part bad?

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

问题描述

我有一个基本的模板,我想分为三个部分:标题,正文,页脚。然后我使用基本模板来包含三个子模板。不过,从我所看到的,这意味着我无法覆盖{{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我的内容
{%endblock%}
< / div>

footer.html:

 < footer> 
{%block footer%}
版权所有2015
{%endblock%}
< / footer>
< / body>
< / html>

base.html:

  {%includeheader.html%} 
{%includebody.html%}
{%includefooter.html%}
< ;! - 和不工作的部分 - >
{%block head_extension%}
< script src =unique_script>< / script>
{%endblock%}
{%block content%}
我的独特内容
{%endblock%}
{%block footer%}
版权所有2011
{%endblock%}
<! - end broken django templateating try - >

我做错了吗?模板文件似乎表明我想要做的不行。看起来这将是创建易于阅读的模板的最佳方法。将所有零件都放在一个大文件中是否更好?您可以想象,标题,正文和页脚元素远远大于此演示。但是这一点依然存在。



我希望有一种办法可以做我不知道的想法。



提前感谢

解决方案

您的方法很好,但您的顺序是错误的。首先,html起始< html> 和结束标签< / html> 不应该分为不同的文件,它是很好的在 base.html



下面是一个例子,如何遵循分解结构:



base.html

 < HTML> 
< head>
<! - 这里的某些东西应该包含在所有模板中,例如js或css - >
{%block extra_css%}
<! - 包含app-template dependent css - >
{%endblock extra_css%}

{%block extra_js%}
<! - 包含app-template dependent js - >
{%endblock extra_js%}

{%block extra_head%}
<! - 对于head中的其他内容 - >
{%endblock extra_head%}

< / head>
< body>
{%block menu%}
<! - 这里的默认菜单 - >
{%block extra_menu%}
<! - 基于模板的扩展菜单 - >
{%endblock extra_menu%}
{%endblock menu%}

{%block content%}
< div>这是很好的< / div>
{%endblock content%}

{%includefooter.html%}

{%block bottom_js%}
<! - - 如果你想在底部有手动的js脚本 - >
{%endblock bottom_js%}
< / body>
< / html>

现在 base.html 现在都设置我们假定您想覆盖 base.html 内容的另一个子模板,您将会执行以下操作:



child.html

  {%extendsbase .html%

{%block content%}
< div>这真的很好< / div>
{%endblock content%}

所以当页面加载时,你会看到这是非常好的而不是这是很好的(在base.html内部块中定义),因为你只是覆盖



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



child.html

  {%extendsbase.html%} 

{%block content%}
{{block.super}}
< div>这真的很好/ DIV>
{%endblock content%}

现在,您将看到这是很好的这真的很好。希望这将澄清你的想法,并将带领你的好。


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天全站免登陆