Template Inheritance (Extend)
The most powerful and complex part of the Django template engine is the template inheritance. Template inheritance lets you create a basic "skeleton" template that contains all the elements in your site, and can define blocks that can be overwritten by a quilt template.
By starting with this example, you can easily understand the template inheritance:
<! DOCTYPE html>
This template, which we call it base.html
, defines a simple HTML skeleton that can be used for two-column layout pages. The work of the "sub-template" is to fill the empty blocks with their contents.
In this example, the block
tag defines three blocks that can be filled with template content. block
tell the template engine: the sub-template may overwrite these locations in the template template.
The sub-template may look like this:
12345678910 |
{
%
extends
"base.html"
%
}
{
%
block title
%
}My amazing blog{
%
endblock
%
}
{
%
block content
%
}
{
%
for
entry
in
blog_entries
%
}
/
h2>
<p>{{ entry.body }}<
/
p>
{
%
endfor
%
}
{
%
endblock
%
}
|
extends
Tags are the key here. It tells the template engine that the template "Inherits" another template. When the template system processes the template, first, it locates the parent template-in this case, the "base.html".
At that point, the template engine will take note base.html
of the three tags in and replace the blocks with the contents of the block
sub-template. Depending on blog_entries
the value, the output might look like this:
123456789101112131415161718192021222324 |
<!DOCTYPE html>
=
"en"
>
<link rel
=
"stylesheet"
href
=
"style.css"
/
>
<title>My amazing blog<
/
title>
<
/
head>
<body>
<div
id
=
"sidebar"
>
<ul>
<li><a href
=
"/"
>Home<
/
a><
/
li>
<li><a href
=
"/blog/"
>Blog<
/
a><
/
li>
<
/
ul>
<
/
div>
<div
id
=
"content"
>
/
h2>
<p>This
is
my first entry.<
/
p>
/
h2>
<p>This
is
my second entry.<
/
p>
<
/
div>
<
/
body>
<
/
html>
|
Note that the sub-template does not have sidebar
a block defined, so the system uses the values from the parent template. The content in the parent template's {% block %}
label is always used as an alternative (fallback).
This approach maximizes the reuse of code and makes it easier to add content to a shared content area, for example, partial-scope navigation.
Here are some hints for using inheritance:
If you use a tag in a template {% extends %}
, it must be the first tag in the template. In any other case, template inheritance will not work.
The more labels you set in the base template, the better {% block %}
. Keep in mind that the sub-template does not have to define all the blocks in the parent template, so you can populate the most blocks with the appropriate default content, and then define only the one you need. A little more hooks is better than a little bit less.
If you find yourself copying content in a large number of templates, it might mean that you should move the content to one of the parent templates {% block %}
.
-
If You need to get the content of the block from the parent template, the {{block.super}}
Variable'll do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{block.super}}
will not being automatically escaped (see The next section ), since it was already escaped, if necessary, and in the parent template.
-
For better readability, you can also give your , {% endblock%}
tag a name . Example:
123 |
{ % block content % ... { % Endblock content % } |
In a large template, this method helps you see which clearly; {% block%}
tag is closed.
Finally, note that you cannot define multiple labels of the same name in one template block
. This limitation exists because the block tag acts as a "two-way". This means that the block tag not only provides a pit to fill, it also defines the contents of the pits in the _ Parent template _. If you have two names in a template block
, the parent template of the template will not know which block to use.
Template inheritance for the Django web framework