Ticket #16335: defaultdict_docs.diff

File defaultdict_docs.diff, 935 bytes (added by Nick Meharry, 13 years ago)

Add example of current template behavior w.r.t. defaultdicts.

  • docs/topics/templates.txt

     
    9797        * Method call
    9898        * List-index lookup
    9999
     100    This can cause some unexpected behavior with objects that override
     101    dictionary lookup. For example, consider the following code snippet that
     102    attempts to loop over a ``collections.defaultdict``::
     103
     104        {% for k, v in defaultdict.iteritems %}
     105            Do something with k and v here...
     106        {% endfor %}
     107
     108    Because dictionary lookup happens first, that behavior kicks in and provides
     109    a default value instead of using the intended ``.iteritems()``
     110    method. In this case, consider converting to a dictionary first.
     111
    100112In the above example, ``{{ section.title }}`` will be replaced with the
    101113``title`` attribute of the ``section`` object.
    102114
Back to Top