Ticket #12199: firstof_as.diff

File firstof_as.diff, 3.3 KB (added by Tim Valenta, 14 years ago)

Patch to defaulttags.py and templatetag docs

  • django/template/defaulttags.py

     
    8686        return filtered
    8787
    8888class FirstOfNode(Node):
    89     def __init__(self, vars):
     89    def __init__(self, vars, variable_name=None):
    9090        self.vars = vars
     91        self.variable_name = variable_name
    9192
    9293    def render(self, context):
    9394        for var in self.vars:
    9495            value = var.resolve(context, True)
    9596            if value:
    96                 return smart_unicode(value)
     97                if self.variable_name:
     98                    context[self.variable_name] = value
     99                    break
     100                else:
     101                    return smart_unicode(value)
    97102        return u''
    98103
    99104class ForNode(Node):
     
    614619
    615620        {% filter force_escape %}
    616621            {% firstof var1 var2 var3 "fallback value" %}
    617         {% endfilter %}
     622        {% endfilter %}
    618623
     624    If you need to preserve the result instead of printing it to the output,
     625    you may leverage the familiar Python keyword ``as``::
     626
     627        {% firstof substitute teacher as teacher_for_today %}
     628
     629    Consider the following template code, which would prove unreliable if not
     630    using the ``as`` syntax::
     631
     632        Today the teacher is {% firstof substitute.name teacher.name %},
     633        and drives a {% firstof substitute.car teacher.car %}
     634
     635    If ``substitute`` doesn't have a car, the second tag would give incorrect
     636    information about what ``substitute`` drives.  Use the ``as`` syntax when
     637    multiple fields should be tested on the single dominant object of a
     638    ``{% firstof %}`` usage.
     639
    619640    """
    620641    bits = token.split_contents()[1:]
     642    variable_name = None
     643    expecting_save_as = bits[-2] == 'as'
     644    if expecting_save_as:
     645        variable_name = bits.pop(-1)
     646        bits = bits[:-1]
    621647    if len(bits) < 1:
    622648        raise TemplateSyntaxError("'firstof' statement requires at least one"
    623649                                  " argument")
    624     return FirstOfNode([parser.compile_filter(bit) for bit in bits])
     650    return FirstOfNode([parser.compile_filter(bit) for bit in bits], variable_name)
    625651firstof = register.tag(firstof)
    626652
    627653#@register.tag(name="for")
  • docs/ref/templates/builtins.txt

     
    209209        {% firstof var1 var2 var3 "fallback value" %}
    210210    {% endfilter %}
    211211
     212.. versionchanged:: 1.2
     213
     214If you need to preserve the result instead of printing it to the output,
     215you may leverage the familiar Python keyword ``as``::
     216
     217    {% firstof substitute teacher as teacher_for_today %}
     218
     219Consider the following template code, which would prove unreliable if not
     220using the ``as`` syntax::
     221
     222    Today the teacher is {% firstof substitute.name teacher.name %},
     223    and drives a {% firstof substitute.car teacher.car %}
     224
     225If ``substitute`` doesn't have a car, the second tag would give incorrect
     226information about what ``substitute`` drives.  Use the ``as`` syntax when
     227multiple fields should be tested on the single dominant object of a
     228``{% firstof %}`` usage.
     229
    212230.. templatetag:: for
    213231
    214232for
Back to Top