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

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

问题描述

Yo

所以我有一个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,我们可以看到我所拥有的snazzy代码正在重复 - 两次(为了重复的乐趣!)

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

我不想重复这个代码 - 我的意思是,如果这将是一个主要的麻烦,我会,但否则我想要一个页面有snazzy代码定义,然后将上面的小变化(可能)放在下面。

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

等其他的东西也是!

好的,所以我知道我可以发送一个参数与一个不同的标题或你有什么 - 让我们假装上面的Snazzy的东西是,我不知道,显示一些其他模板我想要或做一些不平凡的事情。是我在方式之前详细说明的吗?

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 have a much in common, for example they are pages of some section of your site, or just have very common strucure, the way you described is the best. Make another base template, add more blocks and inherit form it. I just think you should use some more descriptive names for block, for example

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


  • 如果您的模板没有共同点,但分享一些特定的块的内容,是另一种情况。这里很难做出适当的继承结构。因此,您可以在这里使用 {%include%} :例如,制作另一个模板显示帖子(显示所有帖子的snazzy代码),说 _list_posts.html
    您可以制作另一个显示帖子的模板(显示所有帖子的snazzy代码),例如 _list_posts.html 。然后在儿童模板中使用它。

  • if your templates doesn't have mush in common, but shares some specific block of content, it's another situation. It's difficult to make proper inheritance structure here. So you can use {% include %} here: For example, make another template that shows posts (snazzy code that shows all the posts), say _list_posts.html. You can make another template that shows posts (snazzy code that shows all the posts), for example _list_posts.html. Then use it in children templates.

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

    您还可以使用包含标记

    选择哪种方式?尝试回答这个问题:可以(和应该)这两个模板有共同的祖先?如果是,请转到第一个变体,如果不是 - 第二个。

    Which way to choose? Try to answer the question: can (and should) these two templates have common ancestor? If yes, go for the first variation, if no - for the second.

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

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