Ticket #9456: with.patch

File with.patch, 3.4 KB (added by eibaan, 7 years ago)
  • django/template/defaulttags.py

     
    407407        return str(int(round(ratio)))
    408408
    409409class WithNode(Node):
    410     def __init__(self, var, name, nodelist):
    411         self.var = var
    412         self.name = name
     410    def __init__(self, varlist, namelist, nodelist):
     411        self.varlist = varlist
     412        self.namelist = namelist
    413413        self.nodelist = nodelist
    414414
    415415    def __repr__(self):
    416416        return "<WithNode>"
    417417
    418418    def render(self, context):
    419         val = self.var.resolve(context)
    420419        context.push()
    421         context[self.name] = val
     420        for name, var in zip(self.namelist, self.varlist):
     421            context[name] = var.resolve(context)
    422422        output = self.nodelist.render(context)
    423423        context.pop()
    424424        return output
     
    11151115def do_with(parser, token):
    11161116    """
    11171117    Adds a value to the context (inside of this block) for caching and easy
    1118     access.
     1118    access. You bind a value to a new name using ``as``. You can chain multiple
     1119    bindings with ``and`` which are evaluated sequentially.
    11191120
    11201121    For example::
    11211122
    11221123        {% with person.some_sql_method as total %}
    11231124            {{ total }} object{{ total|pluralize }}
    11241125        {% endwith %}
     1126       
     1127        {% with value1 as var1 and value2 as var2 %}
     1128            {{ var1 }} ... {{ var2 }}
     1129        {% endwith %}
    11251130    """
    11261131    bits = list(token.split_contents())
    1127     if len(bits) != 4 or bits[2] != "as":
    1128         raise TemplateSyntaxError("%r expected format is 'value as name'" %
     1132    if len(bits) % 4 or any(b != "as" for b in bits[2::4]):
     1133        raise TemplateSyntaxError("%r expected format is 'value as name {and value as name}...'" %
    11291134                                  bits[0])
    1130     var = parser.compile_filter(bits[1])
    1131     name = bits[3]
     1135    varlist = [parser.compile_filter(b) for b in bits[1::4]]
     1136    namelist = bits[3::4]
    11321137    nodelist = parser.parse(('endwith',))
    11331138    parser.delete_first_token()
    1134     return WithNode(var, name, nodelist)
     1139    return WithNode(varlist, namelist, nodelist)
    11351140do_with = register.tag('with', do_with)
  • tests/regressiontests/templates/tests.py

     
    888888            ### WITH TAG ########################################################
    889889            'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
    890890            'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')),
     891            'with03': ('{% with dict.k1 as k1 and dict.k2 as k2 %}{{k1}}+{{k2}}{% endwith %}', {'dict': {'k1':3, 'k2':4}}, '3+4'),
    891892
    892893            'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
    893894            'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
     895            'with-error03': ('{% with dict.k1 as k1 and dict.k2 %}{{ key }}{% endwith %}', {'dict': {'k1':3, 'k2':4}}, template.TemplateSyntaxError),
    894896
    895897            ### NOW TAG ########################################################
    896898            # Simple case
Back to Top