1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Template Inheritance | Jinja Documentation</title>
<style text="text/css">
body {
font-family: 'Arial', sans-serif;
margin: 1em;
padding: 0;
}
#navigation {
float: right;
margin: -1em -1em 1em 1em;
padding: 1em 2em 0 2em;
border: 1px solid #bbb;
border-width: 0 0 1px 1px;
background-color: #f8f8f8;
}
#page {
width: 45em;
}
a {
color: #d00;
}
a:hover {
color: #d40;
}
h1 {
font-size: 2em;
color: #d00;
margin: 0.5em 0 0.5em 0;
padding: 0;
}
h2 {
font-size: 1.7em;
color: #bd2121;
margin: 1em 0 0.5em 0;
}
h3 {
font-size: 1.3em;
color: #8a2424;
margin: 0.5em 0 0 0;
}
p {
margin: 0.5em 1em 0.5em 1em;
}
pre {
margin: 1em 0 1em 2em;
padding: 0.5em;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #f2f2f2;
overflow: auto;
}
li {
line-height: 1.4em;
}
hr {
margin: 1em;
padding: 0;
height: 1px!important;
background-color: #ccc;
border: none!important;
}
div.admonition {
margin: 1em 0 1em 1.5em;
padding: 0.5em 0.5em 0.5em 2em;
background-color: #f6e3e3;
border: 1px solid #d50000;
border-left: none;
border-right: none;
}
div.admonition p.admonition-title {
font-size: 1.1em;
color: #d40;
font-weight: bold;
margin: 0 0 0.5em -1em;
}
table {
border-collapse: collapse;
margin: 1em 2em 1em 1.5em;
}
table td, table th {
text-align: left;
border: 1px solid #eee;
padding: 0.3em;
}
table th {
background-color: #d00000;
color: white;
border: 1px solid #d00000;
border-bottom: 1px solid #eee;
}
</style>
</head>
<body>
<div id="navigation">
<h3>Documentation</h3>
<ul>
<li><a href="index.html">back to index</a></li>
<li><a href="http://wsgiarea.pocoo.org/repos/jinja/trunk/docs/source/inheritance.txt">view source online</a></li>
</ul>
<h3>Table of Contents</h3>
<ul>
<li><a href="#base-template">Base Template</a></li>
<li><a href="#child-template">Child Template</a></li>
</ul>
</div>
<h1>Template Inheritance</h1>
<div id="page">
<p>The most powerful part of Jinja is template inheritance. Template inheritance
allows you to build a base "skeleton" template that contains all the common
elements of your site and defines <strong>blocks</strong> or <strong>markers</strong> that child
templates can override.</p>
<p>Sounds complicated but is very basic. It's easiest to understand it by starting
with an example.</p>
<div class="section">
<h2><a id="base-template" name="base-template">Base Template</a></h2>
<p>This template, which we'll call <tt class="docutils literal"><span class="pre">base.html</span></tt>, defines a simple HTML skeleton
document that you might use for a simple two-column page. It's the job of
"child" templates to fill the empty blocks with content:</p>
<pre class="literal-block">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% marker "title" %} ~ My Webpage</title>
{% marker "htmlhead" %}
</head>
<body>
<div id="content">
{% marker "content" %}
</div>
<div id="footer">
{% block "footer" %}
&copy; Copyright 2006 by <a href="http://mydomain.tld">myself</a>.
{% endblock %}
</div>
</body>
</pre>
<p>In this example, the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> and <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">marker</span> <span class="pre">%}</span></tt> tags define four
blocks that child templates can fill in. All the <tt class="docutils literal"><span class="pre">block</span></tt> tag does is to
tell the template engine that a child template may override those portions
of the template. The <tt class="docutils literal"><span class="pre">marker</span></tt> tag is the same as an empty <tt class="docutils literal"><span class="pre">block</span></tt> tag.</p>
</div>
<div class="section">
<h2><a id="child-template" name="child-template">Child Template</a></h2>
<p>A child template might look like this:</p>
<pre class="literal-block">
{% extends "base" %}
{% marker "title" set "Index" %}
{% block "htmlhead" %}
<style type="text/css">
.important {
color: #336699;
}
</style>
{% endblock %}
{% block "content" %}
<h1>Index</h1>
<p>
Welcome on my awsome homepage.
</p>
{% endblock %}
</pre>
<p>The <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">%}</span></tt> tag is the key here. It tells the template engine that
this template "extends" another template. When the template system evaluates
this template, first it locates the parent.</p>
<p>The filename of the template depends on the template loader. For example the
<tt class="docutils literal"><span class="pre">FileSystemReader</span></tt> allows you to access other templates by giving the
filename without the file extension. You can access subdirectory with an slash:</p>
<pre class="literal-block">
{% extends "layout/default" %}
</pre>
<p>But this behavior can depend on the application using Jinja.</p>
<p>Note that since the child template didn't define the <tt class="docutils literal"><span class="pre">footer</span></tt> block, the
value from the parent template is used instead. Content within a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt>
tag, or the optional default value of a marker tag
<tt class="docutils literal"><span class="pre">{%</span> <span class="pre">marker</span> <span class="pre">"name"</span> <span class="pre">set</span> <span class="pre">"value"</span> <span class="pre">%}</span></tt> in a parent template is always used
as a fallback.</p>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p>You can't define multiple <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tags with the same name in the
same template. This limitation exists because a block tag works in "both"
directions. That is, a block tag doesn't just provide a hole to fill - it
also defines the content that fills the hole in the <em>parent</em>. If there were
two similarly-named <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tags in a template, that template's
parent wouldn't know which one of the blocks' content to use.</p>
<p class="last">If you want to use a block only in one direction but use it twice you can
use the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">capture</span> <span class="pre">%}</span></tt> tag. More information in the <a class="reference" href="./utils.html">utils</a> section.</p>
</div>
</div>
</div>
</body>
</html>
|