django template inheritance

1. Essentially, template inheritance is to construct a basic framework template first, and then overload the common parts and definitions of the sites it contains in its sub-templates. 

 Create the base.html template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title>{% block title %}{% endblock %}</title>
    </head>
    <body>
        <h1>I have a schedule</h1>
        {% block content %}{% endblock %}
        {% block footer %}
        <hr>
        <p>Thank you for your support</p>
        {% endblock %}
    </body>
</html>

 2. Create a sub-template test4.html

{% extends "base.html" %}
{% block title %}Current time{% endblock %}
{% block content %}
<p>The time is now: {{nowtime}}</p>
{% endblock %}

 3. Write the view function (views.py)

def test4(request):
 51     now =datetime.datetime.now()
 52     t=get_template('test4.html')
 53     html=t.render(Context({'nowtime':now }))
 54     return HttpResponse(html)

 4. Write url (url.py)

rl(r'^test4/','mysite2.views.test4'),

 5. Description

 

       looks beautiful doesn't it? Each template contains only code that is unique to itself. No need for extra parts. If you want to make site-level design changes, you only need to modify base.html and all other templates will immediately reflect the changes.

Here's how it works. While loading the current_datetime.html template, the template engine found the {% extends %} tag and noticed that the template was a sub-template. The template engine immediately loads its parent template, which is base.html in this case.

At this point, the template engine notices the three {% block %} tags in base.html and replaces these blocks with the content of the subtemplate. Therefore, the engine will use the title we defined in { block title %} , and the same for {% block content %}. So, the title block of the web page will be replaced by {% block title %}, and similarly, the content block of the web page will be replaced by {% block content %}.

Note that since the child template does not define a footer block, the template system will use the value defined in the parent template. Content inside the parent template's {% block %} tag is always treated as a fallback.

Inheritance does not affect the context of the template. In other words, any template in the inheritance tree has access to every template variable you pass into the template.

You can use as many inheritances as you want. A common way to use inheritance is the following three-tier approach:

  •     Create a base.html template where you define the main look and feel of your site. These are the parts that are changed infrequently or even never.
  •     Create base_SECTION.html templates (eg, base_photos.html and base_forum.html ) for each area of ​​the site. These templates extend base.html and include locale-specific styles and designs.
  •     Create separate templates for each type of page, such as forum pages or image galleries. These templates extend the corresponding area templates.

This approach maximizes code reuse and makes adding content to common areas such as area-level navigation a breeze.

Here are some tips for using template inheritance:

  •     If {% extends %} is used in a template, it must be the first template tag in the template. Otherwise, template inheritance will not work.
  •     In general, the more {% block %} tags in the base template, the better. Remember that child templates don't have to define all the code blocks in the parent template, so you can fill some blocks with sensible defaults, and then (re)define only the code blocks needed by the child template. As the saying goes, the more hooks the better.
  •     If you find yourself copying code between multiple templates, you should consider placing the snippet in a {% block %} of the parent template.
  •     If you need to access the content of a block in the parent template, use the {{ block.super }} tag. This magic variable will represent the content of the parent template. This variable is useful if you only want to add content to the upper block of code, rather than reloading it all.
  •     It is not allowed to define multiple {% block %} with the same name in the same template. This limitation exists because block tags work in both directions. That is to say, the block tag not only digs a hole to be filled, but also defines the content to be filled in the hole in the parent template. If two {% block %} tags with the same name appear in a template, the parent template will have no way of knowing which block's content to use.
  •     {% extends %} uses the same loading method as get_template() for the passed template name. That is, the template name is added after the TEMPLATE_DIRS setting.
  •     In most cases, the argument to {% extends %} should be a string, but it can also be a variable if the parent template name cannot be determined until runtime. This enables you to implement some cool dynamic features.

  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326641204&siteId=291194637