Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

django multiple template inheritance - is this the right style?

Yo

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

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:

{% 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:

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

etc etc for the other pieces too!

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?

cheers!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yo. :-)

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

    You could also use the inclusion tag for that.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...