django 多模板继承 - 这是正确的风格吗? [英] django multiple template inheritance - is this the right style?

查看:15
本文介绍了django 多模板继承 - 这是正确的风格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 base.html:

so i have a base.html:

<html>
    <body>
        <div id="header"> ... </div>
        {% block main %}{% endblock %}
        <div id="footer"> ... </div>
    </body>
</html>

而且我还有一个页面显示用户的帖子:

and i also have a page that shows user's posts:

{% extends base.html %|
{% block main%}
    <h1>welcome to yours posts hangout!</h1>

      ... snazzy code here that shows all the posts ...

{% endblock%}

现在,问题是,也许我还有一个这样的页面:

now, the problem is, maybe i have another page like this:

{% extends base.html %|
{% block main%}
    <h1>look at all posts by all users!</h1>

      ... snazzy code here that shows all the posts by all the users ...

{% endblock%}

因为我们都属于mensa,我们可以看到我拥有的时髦代码正在重复 - 两次(为了同义反复!)

because we all belong to mensa, we can see that the snazzy code i have is being repeated - twice (for tautological fun!)

我不想重复这段代码 - 我的意思是,如果它会成为一个主要的麻烦,我会,但否则我想要一个定义了时髦代码的页面,然后滑动小改动上面和(可能)下面.

i don't want to repeat this code - i mean, if it is going to be a major hassle i will, but otherwise i'd like the one page that has the snazzy code defined, and then slip the small changes above and (possibly) below it in.

我对模板的理解是不稳定的 - 我认为这是去做的方式,有没有更好/标准化的方法?

my understanding of templates is shaky though - i think this is the way to go about doing it, is there a better/standardised way?

snazzy.html:

snazzy.html:

{% extends base.html %|
  {% block aboveSnazzy%}
  {% endblock %}

      ... snazzy code here that shows all the posts by all the users ...

  {% block belowSnazzy%}
  {% endblock %}
{% endblock%}

然后对于每个不同的部分,我可以有:

and then for each of the different pieces, i can have:

usersArea.html:

usersArea.html:

{% extends snazzy.html %|
  {% block aboveSnazzy%}
      <h1>welcome to yours posts hangout!</h1>
  {% endblock %}


  {% block belowSnazzy%}
      <h1>i didn't think this far ahead in the example</h1>
  {% endblock %}
{% endblock%}

其他部分的等等等等!

好的,所以我知道我可以只发送一个带有不同标题的参数或者你有什么 - 让我们假设上面的时髦的东西是,我不知道,展示一些我想要的其他模板或做一些非-不重要的.我在上面详述的方法"是做什么的?

ok, so i know i can just send in a parameter with a different header or what have you - let's pretend that the aboveSnazzy stuff is, i don't know, showing some other template i'd like or doing something non-trivial. Is what i've detailed above the "way" to do it?

干杯!

推荐答案

哟.:-)

答案取决于您的模板有多少共同点.

The answer depends on how much your templates have in common.

  • 如果您的模板确实有很多共同点,即它们是您网站某个部分的页面,或者只是具有非常通用的结构,那么您的做法就是正确的.我只是认为您应该为块使用更具描述性的名称.

  • If your templates do have much in common, i.e. they are pages of some section of your site, or just have a very common structure, then your way of doing it would be correct. I just think that you should use more descriptive names for the blocks.

{% extends base.html %}
{% block page_heading %}{% endblock %}
    ... snazzy code here that shows all the posts by all the users ...
{% block extra_content %}{% endblock %}

  • 如果您的模板没有太多共同点,但共享一些特定的内容块,那么情况就不同了,因为很难制作适当的继承结构.您应该使用 {% include %} 标签这个案例.例如,制作另一个显示帖子的模板,比如 _list_posts.html,然后在子模板中使用它.

  • If your templates don't have much in common, but share some specific block of content, then it's a different situation since it's difficult to make a properly inherited structure. You should use the {% include %} tag in this case. For example, make another template that shows posts, say _list_posts.html, then use it in the children templates.

    {% extends base.html %}
    {% block main %}
    <h1>Welcome to your posts hangout!</h1>
    {% include '_list_posts.html' %}
    {% endblock %}
    

    您也可以使用包含标签 为此.

    那么,您应该选择哪个选项?试着回答这个问题:这两个模板应该有一个共同的父级吗?如果是,请选择第一个选项.否则,请选择第二个选项.

    So, which option should you choose? Try to answer the question: should these two templates have a common parent? If yes, go for the first option. Otherwise, go for the second option.

    这篇关于django 多模板继承 - 这是正确的风格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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