Ticket #4123: 4123.diff

File 4123.diff, 2.2 KB (added by Matt Boersma, 17 years ago)

Patch with docs and tests for handling strings with spaces in the firstof tag.

  • django/template/defaulttags.py

     
    517517        {% endif %}{% endif %}{% endif %}
    518518
    519519    but obviously much cleaner!
     520
     521    You can also use a literal string as a fallback value in case all
     522    passed variables are False::
     523
     524        {% firstof var1 var2 var3 "fallback value" %}
     525
    520526    """
    521     bits = token.contents.split()[1:]
     527    bits = token.split_contents()[1:]
    522528    if len(bits) < 1:
    523529        raise TemplateSyntaxError, "'firstof' statement requires at least one argument"
    524530    return FirstOfNode(bits)
  • tests/regressiontests/templates/tests.py

     
    341341            'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'),
    342342            'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'),
    343343            'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'),
    344             'firstof06': ('{% firstof %}', {}, template.TemplateSyntaxError),
     344            'firstof06': ('{% firstof a b c %}', {'b':0,'c':3}, '3'),
     345            'firstof07': ('{% firstof a b "c" %}', {'a':0}, 'c'),
     346            'firstof08': ('{% firstof a b "c and d" %}', {'a':0,'b':0}, 'c and d'),
     347            'firstof09': ('{% firstof %}', {}, template.TemplateSyntaxError),
    345348
    346349            ### FOR TAG ###############################################################
    347350            'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"),
  • docs/templates.txt

     
    453453    {% else %}{% if var3 %}
    454454        {{ var3 }}
    455455    {% endif %}{% endif %}{% endif %}
     456   
     457You can also use a literal string as a fallback value in case all
     458passed variables are False::
    456459
     460    {% firstof var1 var2 var3 "fallback value" %}
     461
    457462for
    458463~~~
    459464
Back to Top