Changeset 2798
- Timestamp:
- 04/30/06 22:39:59 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/docs/templates_python.txt
r2744 r2798 379 379 Your templates can go anywhere you want, as long as the directories and 380 380 templates are readable by the Web server. They can have any extension you want, 381 such as ``.html`` or ``.txt`` or whatever.381 such as ``.html`` or ``.txt``, or they can have no extension at all. 382 382 383 383 Note that these paths should use Unix-style forward slashes, even on Windows. … … 397 397 of template names. Of the list, it returns the first template that exists. 398 398 399 For example, if you call ``get_template( "story_detail.html")`` and have the399 For example, if you call ``get_template('story_detail.html')`` and have the 400 400 above ``TEMPLATE_DIRS`` setting, here are the files Django will look for, in 401 401 order: … … 404 404 * ``/home/html/templates/default/story_detail.html`` 405 405 406 If you call ``select_template([ "story_253_detail.html", "story_detail.html"])``,406 If you call ``select_template(['story_253_detail.html', 'story_detail.html'])``, 407 407 here's what Django will look for: 408 408 … … 416 416 .. admonition:: Tip 417 417 418 You can use ``select_template `` for super-flexible "templatability." For418 You can use ``select_template()`` for super-flexible "templatability." For 419 419 example, if you've written a news story and want some stories to have 420 420 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'])``. 422 422 That'll allow you to use a custom template for an individual story, with a 423 423 fallback template for stories that don't have custom templates. … … 435 435 To load a template that's within a subdirectory, just use a slash, like so:: 436 436 437 get_template("news/story_detail.html") 437 get_template('news/story_detail.html') 438 439 Using 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`` 438 444 439 445 Loader types … … 441 447 442 448 By 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: 449 with a few other template loaders, which know how to load templates from other 450 sources. 451 452 These other loaders are disabled by default, but you can activate them by 453 editing your ``TEMPLATE_LOADERS`` setting. ``TEMPLATE_LOADERS`` should be a 454 tuple of strings, where each string represents a template loader. Here are the 455 template loaders that come with Django: 447 456 448 457 ``django.template.loaders.filesystem.load_template_source`` … … 461 470 INSTALLED_APPS = ('myproject.polls', 'myproject.music') 462 471 463 ...then ``get_template( "foo.html")`` will look for templates in these472 ...then ``get_template('foo.html')`` will look for templates in these 464 473 directories, in this order: 465 474 … … 521 530 filters are registered. So, near the top of your module, put the following:: 522 531 523 from django .coreimport template532 from django import template 524 533 525 534 register = template.Library() … … 528 537 529 538 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`` and531 ``django/ core/template/defaulttags.py``, respectively.539 and tags. They're in ``django/template/defaultfilters.py`` and 540 ``django/template/defaulttags.py``, respectively. 532 541 533 542 Writing custom template filters … … 632 641 object:: 633 642 634 from django .coreimport template643 from django import template 635 644 def do_current_time(parser, token): 636 645 try: … … 679 688 Continuing the above example, we need to define ``CurrentTimeNode``:: 680 689 681 from django .coreimport template690 from django import template 682 691 import datetime 683 692 class CurrentTimeNode(template.Node): … … 867 876 For more examples of complex rendering, see the source code for ``{% if %}``, 868 877 ``{% for %}``, ``{% ifequal %}`` and ``{% ifchanged %}``. They live in 869 ``django/ core/template/defaulttags.py``.878 ``django/template/defaulttags.py``.
