Django

Code

Changeset 2798

Show
Ignore:
Timestamp:
04/30/06 22:39:59 (2 years ago)
Author:
adrian
Message:

magic-removal: Quickly proofread docs/templates_python.txt

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/docs/templates_python.txt

    r2744 r2798  
    379379Your templates can go anywhere you want, as long as the directories and 
    380380templates are readable by the Web server. They can have any extension you want, 
    381 such as ``.html`` or ``.txt`` or whatever
     381such as ``.html`` or ``.txt``, or they can have no extension at all
    382382 
    383383Note that these paths should use Unix-style forward slashes, even on Windows. 
     
    397397    of template names. Of the list, it returns the first template that exists. 
    398398 
    399 For example, if you call ``get_template("story_detail.html")`` and have the 
     399For example, if you call ``get_template('story_detail.html')`` and have the 
    400400above ``TEMPLATE_DIRS`` setting, here are the files Django will look for, in 
    401401order: 
     
    404404    * ``/home/html/templates/default/story_detail.html`` 
    405405 
    406 If you call ``select_template(["story_253_detail.html", "story_detail.html"])``, 
     406If you call ``select_template(['story_253_detail.html', 'story_detail.html'])``, 
    407407here's what Django will look for: 
    408408 
     
    416416.. admonition:: Tip 
    417417 
    418     You can use ``select_template`` for super-flexible "templatability." For 
     418    You can use ``select_template()`` for super-flexible "templatability." For 
    419419    example, if you've written a news story and want some stories to have 
    420420    custom templates, use something like 
    421     ``select_template(["story_%s_detail.html" % story.id, "story_detail.html"])``. 
     421    ``select_template(['story_%s_detail.html' % story.id, 'story_detail.html'])``. 
    422422    That'll allow you to use a custom template for an individual story, with a 
    423423    fallback template for stories that don't have custom templates. 
     
    435435To load a template that's within a subdirectory, just use a slash, like so:: 
    436436 
    437     get_template("news/story_detail.html") 
     437    get_template('news/story_detail.html') 
     438 
     439Using the same ``TEMPLATE_DIRS`` setting from above, this example 
     440``get_template()`` call will attempt to load the following templates: 
     441 
     442    * ``/home/html/templates/lawrence.com/news/story_detail.html`` 
     443    * ``/home/html/templates/default/news/story_detail.html`` 
    438444 
    439445Loader types 
     
    441447 
    442448By default, Django uses a filesystem-based template loader, but Django comes 
    443 with a few other template loaders. They're disabled by default, but you can 
    444 activate them by editing your ``TEMPLATE_LOADERS`` setting. 
    445 ``TEMPLATE_LOADERS`` should be a tuple of strings, where each string represents 
    446 a template loader. Here are the built-in template loaders: 
     449with a few other template loaders, which know how to load templates from other 
     450sources. 
     451 
     452These other loaders are disabled by default, but you can activate them by 
     453editing your ``TEMPLATE_LOADERS`` setting. ``TEMPLATE_LOADERS`` should be a 
     454tuple of strings, where each string represents a template loader. Here are the 
     455template loaders that come with Django: 
    447456 
    448457``django.template.loaders.filesystem.load_template_source`` 
     
    461470        INSTALLED_APPS = ('myproject.polls', 'myproject.music') 
    462471 
    463     ...then ``get_template("foo.html")`` will look for templates in these 
     472    ...then ``get_template('foo.html')`` will look for templates in these 
    464473    directories, in this order: 
    465474 
     
    521530filters are registered. So, near the top of your module, put the following:: 
    522531 
    523     from django.core import template 
     532    from django import template 
    524533 
    525534    register = template.Library() 
     
    528537 
    529538    For a ton of examples, read the source code for Django's default filters 
    530     and tags. They're in ``django/core/template/defaultfilters.py`` and 
    531     ``django/core/template/defaulttags.py``, respectively. 
     539    and tags. They're in ``django/template/defaultfilters.py`` and 
     540    ``django/template/defaulttags.py``, respectively. 
    532541 
    533542Writing custom template filters 
     
    632641object:: 
    633642 
    634     from django.core import template 
     643    from django import template 
    635644    def do_current_time(parser, token): 
    636645        try: 
     
    679688Continuing the above example, we need to define ``CurrentTimeNode``:: 
    680689 
    681     from django.core import template 
     690    from django import template 
    682691    import datetime 
    683692    class CurrentTimeNode(template.Node): 
     
    867876For more examples of complex rendering, see the source code for ``{% if %}``, 
    868877``{% for %}``, ``{% ifequal %}`` and ``{% ifchanged %}``. They live in 
    869 ``django/core/template/defaulttags.py``. 
     878``django/template/defaulttags.py``.