Ticket #2016: 0003-Adds-documentation-for-object_tag.diff

File 0003-Adds-documentation-for-object_tag.diff, 2.1 KB (added by marcusf, 6 years ago)
  • docs/howto/custom-template-tags.txt

    diff --git a/docs/howto/custom-template-tags.txt b/docs/howto/custom-template-tags.txt
    index c6f7677..6b78711 100644
    a b class, like so:: 
    783783The difference here is that ``do_current_time()`` grabs the format string and
    784784the variable name, passing both to ``CurrentTimeNode3``.
     786Shortcut for loading objects into template variables
     789.. versionadded: 1.2
     791If you only need to retrieve and store an object in a template variable, it
     792might seem cumbersome to write both a renderer and a compilation function
     793for such an easy task.
     795That is why Django provides yet another shortcut -- ``object_tag`` -- which
     796makes it easy to write tags that loads objects into template variables.
     798Its use is very similar to ``simple_tag`` in the way that it takes care of
     799all the argument parsing for you, and only requires a single return value --
     800the object you'd like to insert into the template variable.
     804    def get_latest_polls(max_num):
     805        return Poll.objects.order_by('-pub_date')[:max_num]
     807    register.object_tag(get_latest_polls)
     809Or if you wish to use the Python 2.4 decorator syntax::
     811    @register.object_tag
     812    def get_latest_polls(max_num):
     813        return Poll.objects.order_by('-pub_date')[:max_num]
     815This tag returns the latest ``Poll``-objects, sorted by descending order, and
     816limited by the value of ``max_num``. Its use in a template would look like
     819.. code-block:: html+django
     821    {% get_latest_polls 5 as latest_polls %}
     823Which would retrieve the 5 latest polls and store them inside a template
     824variable named "latest_polls".
     826Note that the following syntax is *mandatory* for all object_tag's:
     828.. code-block:: html+django
     830    {% tag_name [args] as <var_name> %}
     832Where ``args`` is the arguments for the templatetag ``tag_name``, and
     833``var_name`` is the name of the template variable in which the returned object
     834should be stored.
    786836Parsing until another block tag
Back to Top