diff --git a/docs/templates.txt b/docs/templates.txt index c4d1e73e16..070749eda3 100644 --- a/docs/templates.txt +++ b/docs/templates.txt @@ -3,9 +3,9 @@ The Django template language ============================ 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 -you have any exposure to other text-based template languages like Smarty_ or -CheetahTemplate_, you should feel right at home with Django's templates. +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, such as Smarty_ +or CheetahTemplate_, you should feel right at home with Django's templates. .. _Smarty: http://smarty.php.net/ .. _CheetahTemplate: http://www.cheetahtemplate.org/ @@ -13,16 +13,15 @@ CheetahTemplate_, you should feel right at home with Django's templates. What's a template? ================== -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, -XML, CSV, etc.). +A template is simply a text file. All Django templates, by convention, have +".html" extensions, but they can generate any text-based format (HTML, XML, +CSV, etc.). -To actually be useful, a template will contain **variables**, which get replaced -with values from the database when the template is evaluated, and **tags**, -which control the logic of the template. +A template contains **variables**, which get replaced with values when the +template is evaluated, and **tags**, which control the logic of the template. -Below is a minimal template that illustrates the basic parts of a -template. Each element will be explained later in this document.:: +Below is a minimal template that illustrates a few basics. Each element will be +explained later in this document.:: {% extends "base_generic" %} @@ -44,64 +43,77 @@ template. Each element will be explained later in this document.:: .. admonition:: Philosophy 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 - just XML/HTML templates -- at the Journal-World we use it for emails, - Javascript, CSV -- you can use the template language for any text-based + TAL)? We wanted Django's template language to be usable for more than + just XML/HTML templates. At World Online, we use it for e-mails, + Javascript and CSV. You can use the template language for any text-based format. What's a variable? ================== -Variables look like this: ``{{ variable }}``. When the template engine -encounters a variable, it evaluates that variable and replaces the variable with -the result. Many variables will be structures with named attributes; you can -"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. +Variables look like this: ``{{ variable }}``. When the template engine +encounters a variable, it evaluates that variable and replaces it with the +result. -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. See `Using the built-in reference`_, below, for help on finding what variables 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? ================ -Filters look like this: ``{{ name|lower }}``. This displays the value of the -``{{ name }}`` variable after being filtered through the ``lower`` filter which, -as you might have guessed, lowercases the text passed through it. +Filters look like this: ``{{ name|lower }}``. This displays the value of the +``{{ name }}`` variable after being filtered through the ``lower`` filter, +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 -on a water pipe: text enters one side, has some operation performed on it, and -"flows" out the other side. Filters may be "chained"; the output of one filter -applied to the next: ``{{ text|escape|linebreaks }}`` is a common idiom for -escaping text contents and then converting line breaks to ``
`` tags. +Filters can be "chained." The output of one filter applied to the next: +``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents +and then converting line breaks to ``
`` tags. -Certain filters take arguments; a filter argument looks like this: ``{{ -bio|truncatewords:"30" }}``. This will display the first 30 words of the -``bio`` variable. Filter arguments always are in double quotes. +Certain filters take arguments. A filter argument looks like this: +``{{ bio|truncatewords:"30" }}``. This will display the first 30 words of the +``bio`` variable. Filter arguments always are in double quotes. The `Built-in filter reference`_ below describes all the built-in filters. What's a tag? ============= -Tags look like this: ``{% tag %}``. Tags are much more complex than variables: -some create text in the output; some control flow by performing loops, or logic; +Tags look like this: ``{% tag %}``. Tags are more complex than variables: Some +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 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 -engine is template inheritance. In a nutshell, template inheritance allows you -to build a base "skeleton" template that contains all the common elements of -your site and defines **blocks** that child templates can override. +engine is template inheritance. Template inheritance allows you to build a base +"skeleton" template that contains all the common elements of your site and +defines **blocks** that child templates can override. 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::
-{{ entry.body }}
+ {% endfor %} + {% endblock %} - {% for entry in blog_entries %}{{ entry.body - }}
{% endfor %} - - {% endblock %} - -The ``{% extends %}`` tag is the key here; it tells the template engine that -this template "extends" another template. When this template is evaluated, -the first step the template engine will perform is to locate the parent -template -- in this case, "base" (note the dropping of the ".html" -extension). +The ``{% extends %}`` 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 -- in this case, "base" (note the +lack of an ".html" extension in the ``{% extends %}`` tag). 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:: -