Задать вопрос

Тел: +7 965 3737 888

504

Просмотров

0

Ответов

"for" template tag with support for "else" if array is empty

<p>This is a customized version of the default for template tag which takes an optional {% else %} clause that will be displayed if the given array is empty.</p>
from django.template import *
&gt;&gt;&gt; t1 = Template("""
    {% load mytags %}
    {% for athlete in athlete_list %}
        {{ athlete }} 
    {% else %}
        No athlete in list!
    {% endfor %}
    """)
&gt;&gt;&gt; c1 = Context(
    {'athlete_list':
        ['me', 'myself', 'I']
    })
&gt;&gt;&gt; t1.render(c1)
u'me myself I '
&gt;&gt;&gt; c2 = Context({})
&gt;&gt;&gt; t1.render(c2)
u'No athlete in list!'

<p>If you want to automatically override the builtin for template-tag add it to the builtins:</p>
from django.template import add_to_builtins
add_to_builtins('python.path.to.mytags')

Вопрос полезен? Да0/Нет0
file_3534.py(6.7Кб)
None