diff options
Diffstat (limited to 'docs/topics/templates.txt')
| -rw-r--r-- | docs/topics/templates.txt | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/docs/topics/templates.txt b/docs/topics/templates.txt index 9fa6c44dc9..492d23d716 100644 --- a/docs/topics/templates.txt +++ b/docs/topics/templates.txt @@ -1,5 +1,3 @@ -.. _topics-templates: - ============================ The Django template language ============================ @@ -8,7 +6,7 @@ The Django template language This document explains the language syntax of the Django template system. If you're looking for a more technical perspective on how it works and how to - extend it, see :ref:`ref-templates-api`. + extend it, see :doc:`/ref/templates/api`. 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 @@ -28,8 +26,8 @@ or CheetahTemplate_, you should feel right at home with Django's templates. tag for looping, etc. -- but these are not simply executed as the corresponding Python code, and the template system will not execute arbitrary Python expressions. Only the tags, filters and syntax listed below - are supported by default (although you can add :ref:`your own extensions - <howto-custom-template-tags>` to the template language as needed). + are supported by default (although you can add :doc:`your own extensions + </howto/custom-template-tags>` to the template language as needed). .. _`The Django template language: For Python programmers`: ../templates_python/ .. _Smarty: http://smarty.php.net/ @@ -140,7 +138,7 @@ template filters: If ``value`` isn't provided or is empty, the above will display "``nothing``". - + :tfilter:`length` Returns the length of the value. This works for both strings and lists; for example:: @@ -148,7 +146,7 @@ template filters: {{ value|length }} If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``. - + :tfilter:`striptags` Strips all [X]HTML tags. For example:: @@ -161,7 +159,7 @@ Again, these are just a few examples; see the :ref:`built-in filter reference <ref-templates-builtins-filters>` for the complete list. You can also create your own custom template filters; see -:ref:`howto-custom-template-tags`. +:doc:`/howto/custom-template-tags`. Tags ==== @@ -217,7 +215,7 @@ Again, the above is only a selection of the whole list; see the :ref:`built-in tag reference <ref-templates-builtins-tags>` for the complete list. You can also create your own custom template tags; see -:ref:`howto-custom-template-tags`. +:doc:`/howto/custom-template-tags`. Comments ======== @@ -571,6 +569,45 @@ This doesn't affect what happens to data coming from the variable itself. The variable's contents are still automatically escaped, if necessary, because they're beyond the control of the template author. +.. _template-accessing-methods: + +Accessing method calls +====================== + +Most method calls attached to objects are also available from within templates. +This means that templates have access to much more than just class attributes +(like field names) and variables passed in from views. For example, the Django +ORM provides the :ref:`"entry_set"<topics-db-queries-related>` syntax for +finding a collection of objects related on a foreign key. Therefore, given +a model called "comment" with a foreign key relationship to a model called +"task" you can loop through all comments attached to a given task like this:: + + {% for comment in task.comment_set.all %} + {{ comment }} + {% endfor %} + +Similarly, :doc:`QuerySets<ref/models/querysets>` provide a ``count()`` method +to count the number of objects they contain. Therefore, you can obtain a count +of all comments related to the current task with:: + + {{ task.comment_set.all.count }} + +And of course you can easily access methods you've explicitly defined on your +own models:: + + # In model + class Task(models.Model): + def foo(self): + return "bar" + + # In template + {{ task.foo }} + +Because Django intentionally limits the amount of logic processing available +in the template language, it is not possible to pass arguments to method calls +accessed from within templates. Data should be calculated in views, then passed +to templates for display. + .. _template-built-in-reference: Using the built-in reference @@ -634,8 +671,8 @@ The ``{% load %}`` tag can take multiple library names, separated by spaces. Example:: {% load comments i18n %} - -See :ref:`howto-custom-template-tags` for information on writing your own custom + +See :doc:`/howto/custom-template-tags` for information on writing your own custom template libraries. Custom libraries and template inheritance |
