Changeset 2794
- Timestamp:
- 04/29/06 14:13:06 (2 years ago)
- Files:
-
- django/branches/magic-removal/docs/templates.txt (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/docs/templates.txt
r2744 r2794 14 14 ========= 15 15 16 A template is simply a text file. All Django templates, by convention, have 17 ".html" extensions, but they can generate any text-based format (HTML, XML, 18 CSV, etc.). 16 A template is simply a text file. It can generate any text-based format (HTML, 17 XML, CSV, etc.). 19 18 20 19 A template contains **variables**, which get replaced with values when the … … 24 23 explained later in this document.:: 25 24 26 {% extends "base_generic " %}25 {% extends "base_generic.html" %} 27 26 28 27 {% block title %}{{ section.title }}{% endblock %} … … 49 48 format. 50 49 50 Oh, and one more thing: Making humans edit XML is masochistic! 51 51 52 Variables 52 53 ========= … … 75 76 (the empty string) by default. 76 77 77 If you use a variable that doesn't exist, it will be silently ignored. The78 variable will be replaced by nothingness.79 80 78 See `Using the built-in reference`_, below, for help on finding what variables 81 79 are available in a given template. 82 80 83 You can modify variables for display by using **filters**.84 85 81 Filters 86 82 ======= 83 84 You can modify variables for display by using **filters**. 87 85 88 86 Filters look like this: ``{{ name|lower }}``. This displays the value of the … … 90 88 which converts text to lowercase. Use a pipe (``|``) to apply a filter. 91 89 92 Filters can be "chained." The output of one filter applied to the next:93 ``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents 94 andthen converting line breaks to ``<p>`` tags.95 96 Certainfilters take arguments. A filter argument looks like this:90 Filters can be "chained." The output of one filter is applied to the next. 91 ``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents, 92 then converting line breaks to ``<p>`` tags. 93 94 Some filters take arguments. A filter argument looks like this: 97 95 ``{{ bio|truncatewords:"30" }}``. This will display the first 30 words of the 98 96 ``bio`` variable. Filter arguments always are in double quotes. … … 155 153 A child template might look like this:: 156 154 157 {% extends "base " %}155 {% extends "base.html" %} 158 156 159 157 {% block title %}My amazing blog{% endblock %} … … 168 166 The ``{% extends %}`` tag is the key here. It tells the template engine that 169 167 this template "extends" another template. When the template system evaluates 170 this template, first it locates the parent -- in this case, "base " (note the171 lack of an ".html" extension in the ``{% extends %}`` tag). 172 173 At that point, the template engine will notice the three blocks in 174 ``base.html`` and replace those blocks with the contents of the child template. 175 Depending on the value of ``blog_entries``, the output might looklike::168 this template, first it locates the parent -- in this case, "base.html". 169 170 At that point, the template engine will notice the three ``{% block %}`` tags 171 in ``base.html`` and replace those blocks with the contents of the child 172 template. Depending on the value of ``blog_entries``, the output might look 173 like:: 176 174 177 175 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" … … 204 202 tag in a parent template is always used as a fallback. 205 203 206 Template inheritance isn't limited to a single level. Multi-level inheritance 207 is possible and, indeed, quite useful. 204 You can use as many levels of inheritance as needed. One common way of using 205 inheritance is the following three-level approach: 206 207 * Create a ``base.html`` template that holds the main look-and-feel of your 208 site. 209 * Create a ``base_SECTIONNAME.html`` template for each "section" of your 210 site. For example, ``base_news.html``, ``base_sports.html``. These 211 templates all extend ``base.html`` and include section-specific 212 styles/design. 213 * Create individual templates for each type of page, such as a news 214 article or blog entry. These templates extend the appropriate section 215 template. 216 217 This approach maximizes code reuse and makes it easy to add items to shared 218 content areas, such as section-wide navigation. 208 219 209 220 Here are some tips for working with inheritance: 210 221 211 222 * If you use ``{% extends %}`` in a template, it must be the first template 212 tag in that template. 223 tag in that template. Template inheritance won't work, otherwise. 213 224 214 225 * More ``{% block %}`` tags in your base templates are better. Remember, 215 226 child templates don't have to define all parent blocks, so you can fill 216 227 in reasonable defaults in a number of blocks, then only define the ones 217 you need later. 228 you need later. It's better to have more hooks than fewer hooks. 218 229 219 230 * If you find yourself duplicating content in a number of templates, it 220 231 probably means you should move that content to a ``{% block %}`` in a 221 232 parent template. 222 223 * The recommended template layout is to use three levels: a single base224 template for the entire site, a set of mid-level templates for each225 section of the site, and then the individual templates for each view.226 This maximizes code reuse and makes it easier to add items to shared227 content areas (such as section-wide navigation).228 233 229 234 * If you need to get the content of the block from the parent template, … … 242 247 ============================ 243 248 244 Because Django can be used to develop any sort of site, the tags, filters and 245 variables available are different depending on the application. To make it 246 easy to figure out what's available in a given site, the admin interface has a 247 complete reference of all the template goodies available to that site. To get 248 that reference, go to your Django admin interface and click the "Documentation" 249 link in the upper right of any page. 250 251 The reference is integrated into the administration interface for your site(s) 252 and is divided into 4 sections: tags, filters, models, and views. 249 Django's admin interface includes a complete reference of all template tags and 250 filters available for a given site. To see it, go to your admin interface and 251 click the "Documentation" link in the upper right of the page. 252 253 The reference is divided into 4 sections: tags, filters, models, and views. 253 254 254 255 The **tags** and **filters** sections describe all the built-in tags (in fact, … … 261 262 * The name of the view function that generates that view. 262 263 * A short description of what the view does. 263 * The **context**, or a list of variables available in the view .264 * The **context**, or a list of variables available in the view's template. 264 265 * The name of the template or templates that are used for that view. 265 266 … … 267 268 from any page to the documentation page for that view. 268 269 269 Because Django generally revolves arounddatabase objects, the **models**270 Because Django-powered sites usually use database objects, the **models** 270 271 section of the documentation page describes each type of object in the system 271 272 along with all the fields available on that object. … … 301 302 302 303 For example, if a template ``foo.html`` has ``{% load comments %}``, a child 303 template (e.g., one that has ``{% extends foo %}`` will *not* have access to304 the comments template tags and filters. The child template is responsible for 305 its own ``{% load comments %}``.304 template (e.g., one that has ``{% extends "foo.html" %}``) will *not* have 305 access to the comments template tags and filters. The child template is 306 responsible for its own ``{% load comments %}``. 306 307 307 308 This is a feature for the sake of maintainability and sanity. … … 363 364 Signal that this template extends a parent template. 364 365 365 This tag may be used in two ways: ``{% extends "base " %}`` (with quotes) uses366 the literal value "base" as the name of the parent template to extend, or ``{% 367 extend s variable %}`` uses the value of ``variable`` as the name of the parent368 template to extend.366 This tag may be used in two ways: ``{% extends "base.html" %}`` (with quotes) 367 uses the literal value "base.html" as the name of the parent template to 368 extend, or ``{% extends variable %}`` uses the value of ``variable`` as the 369 name of the parent template to extend. 369 370 370 371 See `Template inheritance`_ for more information. … … 486 487 Check if a value has changed from the last iteration of a loop. 487 488 488 The 'ifchanged'block tag is used within a loop. It checks its own rendered489 The ``ifchanged`` block tag is used within a loop. It checks its own rendered 489 490 contents against its previous state and only displays its content if the value 490 491 has changed:: … … 764 765 For example:: 765 766 766 <img src= 'bar.gif' height='10' width='{% widthratio this_value max_value 100 %}'/>767 <img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" /> 767 768 768 769 Above, if ``this_value`` is 175 and ``max_value`` is 200, the the image in the … … 845 846 ~~~~~~~~~~~~~~ 846 847 847 Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102848 bytes, etc).848 Format the value like a 'human-readable' file size (i.e. ``'13 KB'``, 849 ``'4.1 MB'``, ``'102 bytes'``, etc). 849 850 850 851 first … … 894 895 ~~~~~~~~~~ 895 896 896 Converts newlines into <p> and <br />s.897 Converts newlines into ``<p>`` and ``<br />``s. 897 898 898 899 linebreaksbr 899 900 ~~~~~~~~~~~~ 900 901 901 Converts newlines into <br />s.902 Converts newlines into ``<br />``s. 902 903 903 904 linenumbers … … 937 938 ~~~~~~~~~ 938 939 939 Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'. 940 Returns ``'s'`` if the value is not 1. 941 942 Example:: 943 944 You have {{ num_messages }} message{{ num_messages|pluralize }}. 940 945 941 946 pprint
