1
0
mirror of https://github.com/django/django.git synced 2025-07-05 18:29:11 +00:00

magic-removal: Quickly proofread docs/templates_python.txt

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2798 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-05-01 03:39:59 +00:00
parent 5737801a14
commit a1f47af81a

View File

@ -378,7 +378,7 @@ that contain full paths to your template directory(ies). Example::
Your templates can go anywhere you want, as long as the directories and Your templates can go anywhere you want, as long as the directories and
templates are readable by the Web server. They can have any extension you want, templates are readable by the Web server. They can have any extension you want,
such as ``.html`` or ``.txt`` or whatever. such as ``.html`` or ``.txt``, or they can have no extension at all.
Note that these paths should use Unix-style forward slashes, even on Windows. Note that these paths should use Unix-style forward slashes, even on Windows.
@ -396,14 +396,14 @@ Django has two ways to load templates from files:
``select_template`` is just like ``get_template``, except it takes a list ``select_template`` is just like ``get_template``, except it takes a list
of template names. Of the list, it returns the first template that exists. of template names. Of the list, it returns the first template that exists.
For example, if you call ``get_template("story_detail.html")`` and have the For example, if you call ``get_template('story_detail.html')`` and have the
above ``TEMPLATE_DIRS`` setting, here are the files Django will look for, in above ``TEMPLATE_DIRS`` setting, here are the files Django will look for, in
order: order:
* ``/home/html/templates/lawrence.com/story_detail.html`` * ``/home/html/templates/lawrence.com/story_detail.html``
* ``/home/html/templates/default/story_detail.html`` * ``/home/html/templates/default/story_detail.html``
If you call ``select_template(["story_253_detail.html", "story_detail.html"])``, If you call ``select_template(['story_253_detail.html', 'story_detail.html'])``,
here's what Django will look for: here's what Django will look for:
* ``/home/html/templates/lawrence.com/story_253_detail.html`` * ``/home/html/templates/lawrence.com/story_253_detail.html``
@ -415,10 +415,10 @@ When Django finds a template that exists, it stops looking.
.. admonition:: Tip .. admonition:: Tip
You can use ``select_template`` for super-flexible "templatability." For You can use ``select_template()`` for super-flexible "templatability." For
example, if you've written a news story and want some stories to have example, if you've written a news story and want some stories to have
custom templates, use something like custom templates, use something like
``select_template(["story_%s_detail.html" % story.id, "story_detail.html"])``. ``select_template(['story_%s_detail.html' % story.id, 'story_detail.html'])``.
That'll allow you to use a custom template for an individual story, with a That'll allow you to use a custom template for an individual story, with a
fallback template for stories that don't have custom templates. fallback template for stories that don't have custom templates.
@ -434,16 +434,25 @@ single directory gets messy.
To load a template that's within a subdirectory, just use a slash, like so:: To load a template that's within a subdirectory, just use a slash, like so::
get_template("news/story_detail.html") get_template('news/story_detail.html')
Using the same ``TEMPLATE_DIRS`` setting from above, this example
``get_template()`` call will attempt to load the following templates:
* ``/home/html/templates/lawrence.com/news/story_detail.html``
* ``/home/html/templates/default/news/story_detail.html``
Loader types Loader types
~~~~~~~~~~~~ ~~~~~~~~~~~~
By default, Django uses a filesystem-based template loader, but Django comes By default, Django uses a filesystem-based template loader, but Django comes
with a few other template loaders. They're disabled by default, but you can with a few other template loaders, which know how to load templates from other
activate them by editing your ``TEMPLATE_LOADERS`` setting. sources.
``TEMPLATE_LOADERS`` should be a tuple of strings, where each string represents
a template loader. Here are the built-in template loaders: These other loaders are disabled by default, but you can activate them by
editing your ``TEMPLATE_LOADERS`` setting. ``TEMPLATE_LOADERS`` should be a
tuple of strings, where each string represents a template loader. Here are the
template loaders that come with Django:
``django.template.loaders.filesystem.load_template_source`` ``django.template.loaders.filesystem.load_template_source``
Loads templates from the filesystem, according to ``TEMPLATE_DIRS``. Loads templates from the filesystem, according to ``TEMPLATE_DIRS``.
@ -460,7 +469,7 @@ a template loader. Here are the built-in template loaders:
INSTALLED_APPS = ('myproject.polls', 'myproject.music') INSTALLED_APPS = ('myproject.polls', 'myproject.music')
...then ``get_template("foo.html")`` will look for templates in these ...then ``get_template('foo.html')`` will look for templates in these
directories, in this order: directories, in this order:
* ``/path/to/myproject/polls/templates/foo.html`` * ``/path/to/myproject/polls/templates/foo.html``
@ -520,15 +529,15 @@ To be a valid tag library, the module contain a module-level variable named
``register`` that is a ``template.Library`` instance, in which all the tags and ``register`` that is a ``template.Library`` instance, in which all the tags and
filters are registered. So, near the top of your module, put the following:: filters are registered. So, near the top of your module, put the following::
from django.core import template from django import template
register = template.Library() register = template.Library()
.. admonition:: Behind the scenes .. admonition:: Behind the scenes
For a ton of examples, read the source code for Django's default filters For a ton of examples, read the source code for Django's default filters
and tags. They're in ``django/core/template/defaultfilters.py`` and and tags. They're in ``django/template/defaultfilters.py`` and
``django/core/template/defaulttags.py``, respectively. ``django/template/defaulttags.py``, respectively.
Writing custom template filters Writing custom template filters
------------------------------- -------------------------------
@ -631,7 +640,7 @@ else. In our case, let's say the tag should be used like this::
The parser for this function should grab the parameter and create a ``Node`` The parser for this function should grab the parameter and create a ``Node``
object:: object::
from django.core import template from django import template
def do_current_time(parser, token): def do_current_time(parser, token):
try: try:
# Splitting by None == splitting by spaces. # Splitting by None == splitting by spaces.
@ -678,7 +687,7 @@ has a ``render()`` method.
Continuing the above example, we need to define ``CurrentTimeNode``:: Continuing the above example, we need to define ``CurrentTimeNode``::
from django.core import template from django import template
import datetime import datetime
class CurrentTimeNode(template.Node): class CurrentTimeNode(template.Node):
def __init__(self, format_string): def __init__(self, format_string):
@ -866,4 +875,4 @@ The only new concept here is the ``self.nodelist.render(context)`` in
For more examples of complex rendering, see the source code for ``{% if %}``, For more examples of complex rendering, see the source code for ``{% if %}``,
``{% for %}``, ``{% ifequal %}`` and ``{% ifchanged %}``. They live in ``{% for %}``, ``{% ifequal %}`` and ``{% ifchanged %}``. They live in
``django/core/template/defaulttags.py``. ``django/template/defaulttags.py``.