Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...