Copy-edited docs/templates.txt

git-svn-id: http://code.djangoproject.com/svn/django/trunk@442 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-08-09 22:39:25 +00:00
parent 32fe8e3863
commit 217c746789
1 changed files with 110 additions and 101 deletions

View File

@ -3,9 +3,9 @@ The Django template language
============================ ============================
Django's template language is designed to strike a balance between power and Django's template language is designed to strike a balance between power and
ease; it's designed to feel comfortable to those used to working with HTML. If ease. It's designed to feel comfortable to those used to working with HTML. If
you have any exposure to other text-based template languages like Smarty_ or you have any exposure to other text-based template languages, such as Smarty_
CheetahTemplate_, you should feel right at home with Django's templates. or CheetahTemplate_, you should feel right at home with Django's templates.
.. _Smarty: http://smarty.php.net/ .. _Smarty: http://smarty.php.net/
.. _CheetahTemplate: http://www.cheetahtemplate.org/ .. _CheetahTemplate: http://www.cheetahtemplate.org/
@ -13,16 +13,15 @@ CheetahTemplate_, you should feel right at home with Django's templates.
What's a template? What's a template?
================== ==================
A template is simply a text file. All Django templates by convention have A template is simply a text file. All Django templates, by convention, have
".html" extensions, but they can actually generate any text-based format (HTML, ".html" extensions, but they can generate any text-based format (HTML, XML,
XML, CSV, etc.). CSV, etc.).
To actually be useful, a template will contain **variables**, which get replaced A template contains **variables**, which get replaced with values when the
with values from the database when the template is evaluated, and **tags**, template is evaluated, and **tags**, which control the logic of the template.
which control the logic of the template.
Below is a minimal template that illustrates the basic parts of a Below is a minimal template that illustrates a few basics. Each element will be
template. Each element will be explained later in this document.:: explained later in this document.::
{% extends "base_generic" %} {% extends "base_generic" %}
@ -45,43 +44,53 @@ template. Each element will be explained later in this document.::
Why use a text-based template instead of an XML-based one (like Zope's Why use a text-based template instead of an XML-based one (like Zope's
TAL)? We wanted Django's template language to be usable for more than TAL)? We wanted Django's template language to be usable for more than
just XML/HTML templates -- at the Journal-World we use it for emails, just XML/HTML templates. At World Online, we use it for e-mails,
Javascript, CSV -- you can use the template language for any text-based Javascript and CSV. You can use the template language for any text-based
format. format.
What's a variable? What's a variable?
================== ==================
Variables look like this: ``{{ variable }}``. When the template engine Variables look like this: ``{{ variable }}``. When the template engine
encounters a variable, it evaluates that variable and replaces the variable with encounters a variable, it evaluates that variable and replaces it with the
the result. Many variables will be structures with named attributes; you can result.
"drill down" into these structures with dots (``.``), so in the above example
``{{ section.title }}`` will be replaced with the ``title`` attribute of the
``section`` object.
If you use a variable that doesn't exist, it will be silently ignored; the Use a dot (``.``) to access attributes of a variable.
.. admonition:: Behind the scenes
Technically, when the template system encounters a dot, it tries the
following lookups, in this order:
* Dictionary lookup
* Attribute lookup
* Method call
* List-index lookup
In the above example, ``{{ section.title }}`` will be replaced with the
``title`` attribute of the ``section`` object.
If you use a variable that doesn't exist, it will be silently ignored. The
variable will be replaced by nothingness. variable will be replaced by nothingness.
See `Using the built-in reference`_, below, for help on finding what variables See `Using the built-in reference`_, below, for help on finding what variables
are available in a given template. are available in a given template.
Variables may be modified before being displayed by **filters**. You can modify variables for display by using **filters**.
What's a filter? What's a filter?
================ ================
Filters look like this: ``{{ name|lower }}``. This displays the value of the Filters look like this: ``{{ name|lower }}``. This displays the value of the
``{{ name }}`` variable after being filtered through the ``lower`` filter which, ``{{ name }}`` variable after being filtered through the ``lower`` filter,
as you might have guessed, lowercases the text passed through it. which converts text to lowercase. Use a pipe (``|``) to apply a filter.
We use the pipe character to apply filters to emphasize the analogy with filters Filters can be "chained." The output of one filter applied to the next:
on a water pipe: text enters one side, has some operation performed on it, and ``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents
"flows" out the other side. Filters may be "chained"; the output of one filter and then converting line breaks to ``<p>`` tags.
applied to the next: ``{{ text|escape|linebreaks }}`` is a common idiom for
escaping text contents and then converting line breaks to ``<p>`` tags.
Certain filters take arguments; a filter argument looks like this: ``{{ Certain filters take arguments. A filter argument looks like this:
bio|truncatewords:"30" }}``. This will display the first 30 words of the ``{{ bio|truncatewords:"30" }}``. This will display the first 30 words of the
``bio`` variable. Filter arguments always are in double quotes. ``bio`` variable. Filter arguments always are in double quotes.
The `Built-in filter reference`_ below describes all the built-in filters. The `Built-in filter reference`_ below describes all the built-in filters.
@ -89,19 +98,22 @@ The `Built-in filter reference`_ below describes all the built-in filters.
What's a tag? What's a tag?
============= =============
Tags look like this: ``{% tag %}``. Tags are much more complex than variables: Tags look like this: ``{% tag %}``. Tags are more complex than variables: Some
some create text in the output; some control flow by performing loops, or logic; create text in the output, some control flow by performing loops or logic, and
some load external information into the template to be used by later variables. some load external information into the template to be used by later variables.
Some tags are "block" tags with matching beginning and ending tags (i.e. ``{% tag %} ... tag contents ... {% endtag %}``. The `Built-in tag reference`_ below describes all the built-in tags. Some tags require beginning and ending tags (i.e.
``{% tag %} ... tag contents ... {% endtag %}``). The `Built-in tag reference`_
below describes all the built-in tags. You can create your own tags, if you
know how to write Python code.
Template Inheritance Template inheritance
==================== ====================
The most powerful -- and thus the most complex -- part of Django's template The most powerful -- and thus the most complex -- part of Django's template
engine is template inheritance. In a nutshell, template inheritance allows you engine is template inheritance. Template inheritance allows you to build a base
to build a base "skeleton" template that contains all the common elements of "skeleton" template that contains all the common elements of your site and
your site and defines **blocks** that child templates can override. defines **blocks** that child templates can override.
It's easiest to understand template inheritance by starting with an example:: It's easiest to understand template inheritance by starting with an example::
@ -110,7 +122,7 @@ It's easiest to understand template inheritance by starting with an example::
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
<title>{% block title %}My Amazing Site{% endblock %}</title> <title>{% block title %}My amazing site{% endblock %}</title>
</head> </head>
<body> <body>
@ -128,36 +140,34 @@ It's easiest to understand template inheritance by starting with an example::
</div> </div>
</body> </body>
This template, which we'll call ``base.html`` defines a simple HTML skeleton This template, which we'll call ``base.html``, defines a simple HTML skeleton
document that you might use for a simple two-column page. This template document that you might use for a simple two-column page. It's the job of
won't actually be used directly on any pages, but other "child" templates will "child" templates to fill the empty blocks with content.
extend it and fill in the empty blocks with content.
I've used the ``{% block %}`` tag to define the three blocks that child templates In this example, the ``{% block %}`` tag defines three blocks that child
will fill in. All that the ``block`` tag does is to signal to the template engine templates can fill in. All the ``block`` tag does is to tell the template
that a child template may override those portions of the template. engine that a child template may override those portions of the template.
To use this template, I might define a child template as follows:: A child template might look like this::
{% extends "base" %} {% extends "base" %}
{% block title %}My Amazing Blog{% endblock %} {% block title %}My amazing blog{% endblock %}
{% block content %} {% block content %}
{% for entry in blog_entries %}
{% for entry in blog_entries %} <h2>{{ entry.title }}</h2> <p>{{ entry.body <h2>{{ entry.title }}</h2>
}}</p> {% endfor %} <p>{{ entry.body }}</p>
{% endfor %}
{% endblock %} {% endblock %}
The ``{% extends %}`` tag is the key here; it tells the template engine that The ``{% extends %}`` tag is the key here. It tells the template engine that
this template "extends" another template. When this template is evaluated, this template "extends" another template. When the template system evaluates
the first step the template engine will perform is to locate the parent this template, first it locates the parent -- in this case, "base" (note the
template -- in this case, "base" (note the dropping of the ".html" lack of an ".html" extension in the ``{% extends %}`` tag).
extension).
At that point, the template engine will notice the three blocks in At that point, the template engine will notice the three blocks in
``base.html``, and replace those blocks with the contents of the child template. ``base.html`` and replace those blocks with the contents of the child template.
Depending on the value of ``blog_entries``, the output might look like:: Depending on the value of ``blog_entries``, the output might look like::
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
@ -165,7 +175,7 @@ Depending on the value of ``blog_entries``, the output might look like::
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
<title>My Amazing Blog</title> <title>My amazing blog</title>
</head> </head>
<body> <body>
@ -185,30 +195,29 @@ Depending on the value of ``blog_entries``, the output might look like::
</div> </div>
</body> </body>
Note that since the child template didn't define the ``sidebar`` block, the
value from the parent template is used instead. Content within a ``{% block %}``
tag in a parent template is always used as a fallback.
Template inheritance isn't limited to a single level. Multi-level inheritance
Note that since the child template did not define the ``sidebar`` block, the is possible and, indeed, quite useful.
value from the parent template is used instead.
Template inheritance does not have to be only single-level; multi-level
inheritance is possible, and indeed, quite useful.
Here are some tips for working with inheritance: Here are some tips for working with inheritance:
* More ``{% block %}`` tags in your base templates are better. Remember, * More ``{% block %}`` tags in your base templates are better. Remember,
child templates do not have to define all parent blocks, so you can child templates don't have to define all parent blocks, so you can fill
fill in reasonable defaults in a number of blocks, then only define in reasonable defaults in a number of blocks, then only define the ones
the ones you need later on. you need later.
* If you find yourself reproducing the same content in a number of * If you find yourself duplicating content in a number of templates, it
documents, it probably means you should move that content to a probably means you should move that content to a ``{% block %}`` in a
new ``{% block %}`` in a parent template. parent template.
* We often prefer to use three-level inheritance: a single base template * The recommended template layout is to use three levels: a single base
for the entire site, a set of mid-level templates for each section of template for the entire site, a set of mid-level templates for each
the site, and then the individual templates for each view. This section of the site, and then the individual templates for each view.
maximizes code reuse, and makes it easier to add items to shared This maximizes code reuse and makes it easier to add items to shared
content areas (like section-wide navigation). content areas (such as section-wide navigation).
* If you need to get the content of the block from the parent template, * If you need to get the content of the block from the parent template,
the ``{{ block.super }}`` variable will do the trick. This is useful the ``{{ block.super }}`` variable will do the trick. This is useful
@ -218,20 +227,20 @@ Here are some tips for working with inheritance:
Using the built-in reference Using the built-in reference
============================ ============================
Since Django can be used to develop any sort of site, the tags, filters, and Because Django can be used to develop any sort of site, the tags, filters and
variables available will be different depending on the application. To make it variables available are different depending on the application. To make it
simple to figure out what's available in a given site, the admin interface has a easy to figure out what's available in a given site, the admin interface has a
complete reference of all the template goodies available to that site. complete reference of all the template goodies available to that site.
This documentation is integrated into the administration interface for your The reference is integrated into the administration interface for your site(s)
sites and is divided into 4 sections: tags, filters, models, and views. and is divided into 4 sections: tags, filters, models, and views.
The **tags** and **filters** sections describe all the built-in tags (in fact, The **tags** and **filters** sections describe all the built-in tags (in fact,
the tag and filter references below come directly from those pages) as well as the tag and filter references below come directly from those pages) as well as
any custom tag or filter libraries available. any custom tag or filter libraries available.
The **views** page is perhaps the most valuable. Each URL in your site has a The **views** page is the most valuable. Each URL in your site has a separate
separate entry here, and clicking on a URL will show you: entry here, and clicking on a URL will show you:
* The name of the view function that generates that view. * The name of the view function that generates that view.
* A short description of what the view does. * A short description of what the view does.
@ -241,9 +250,9 @@ separate entry here, and clicking on a URL will show you:
Each view documentation page also has a bookmarklet that you can use to jump Each view documentation page also has a bookmarklet that you can use to jump
from any page to the documentation page for that view. from any page to the documentation page for that view.
Since most of Django revolves around database objects, the **models** section of Because Django generally revolves around database objects, the **models**
the documentation page describes each type of object in the system along with section of the documentation page describes each type of object in the system
all the fields available on that object. along with all the fields available on that object.
Taken together, the documentation pages should tell you every tag, filter, Taken together, the documentation pages should tell you every tag, filter,
variable and object available to you in a given template. variable and object available to you in a given template.
@ -251,8 +260,8 @@ variable and object available to you in a given template.
Custom tag and filter libraries Custom tag and filter libraries
=============================== ===============================
As mentioned above, certain applications will provide custom tag and filter Certain applications provide custom tag and filter libraries. To access them in
libraries. To access them in a template, use the ``{% load %}`` tag:: a template, use the ``{% load %}`` tag::
{% load comments %} {% load comments %}
@ -265,10 +274,10 @@ area in your admin to find the list of custom libraries in your installation.
Built-in tag and filter reference Built-in tag and filter reference
================================= =================================
For those without an admin site available, the reference for the stock tags and For those without an admin site available, reference for the stock tags and
filters follows. Since Django is highly customizable, the documentation filters follows. Because Django is highly customizable, the reference in your
references in your admin should be considered the final word on what admin should be considered the final word on what tags and filters are
tags and filters are available and their functions. available, and what they do.
Built-in tag reference Built-in tag reference
---------------------- ----------------------
@ -467,7 +476,7 @@ Built-in tag reference
* Bill Clinton * Bill Clinton
* Female: * Female:
* Margaret Thatcher * Margaret Thatcher
* Colendeeza Rice * Condoleezza Rice
* Unknown: * Unknown:
* Pat Smith * Pat Smith