Ticket #6378: 6378-into.diff

File 6378-into.diff, 3.8 KB (added by Eric Holscher, 15 years ago)

Initial patch that is a bit rough.

  • django/template/defaulttags.py

     
    416416        return str(int(round(ratio)))
    417417
    418418class WithNode(Node):
    419     def __init__(self, var, name, nodelist):
     419    def __init__(self, var, name, nodelist, into):
    420420        self.var = var
    421421        self.name = name
    422422        self.nodelist = nodelist
     423        self.into = into
    423424
    424425    def __repr__(self):
    425426        return "<WithNode>"
     
    430431        context[self.name] = val
    431432        output = self.nodelist.render(context)
    432433        context.pop()
    433         return output
     434        if not self.into:
     435            return output
     436        else:
     437            context[self.into] = output
     438            return ''
    434439
    435440#@register.tag
    436441def autoescape(parser, token):
     
    11601165        {% with person.some_sql_method as total %}
    11611166            {{ total }} object{{ total|pluralize }}
    11621167        {% endwith %}
     1168
     1169        {% with person.some_sql_method as total into content %}
     1170            {{ total }} object{{ total|pluralize }}
     1171        {% endwith %}
     1172        {{ content }}
     1173
    11631174    """
    11641175    bits = list(token.split_contents())
    1165     if len(bits) != 4 or bits[2] != "as":
    1166         raise TemplateSyntaxError("%r expected format is 'value as name'" %
     1176    if len(bits) == 4:
     1177        if bits[2] != "as":
     1178            raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" %
    11671179                                  bits[0])
     1180    elif len(bits) == 6:
     1181        if bits[2] != "as" or bits[4] != "into":
     1182            raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" %
     1183                                  bits[0])
     1184    else:
     1185        raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" %
     1186                                  bits[0])
     1187
    11681188    var = parser.compile_filter(bits[1])
    11691189    name = bits[3]
    11701190    nodelist = parser.parse(('endwith',))
    11711191    parser.delete_first_token()
    1172     return WithNode(var, name, nodelist)
     1192    if len(bits) == 4:
     1193        return WithNode(var, name, nodelist, '')
     1194    elif len(bits) == 6:
     1195        into = bits[5]
     1196        return WithNode(var, name, nodelist, into)
     1197
    11731198do_with = register.tag('with', do_with)
  • tests/regressiontests/templates/tests.py

     
    891891            ### WITH TAG ########################################################
    892892            'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
    893893            'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')),
     894            'with03': ('{% with dict.key as key into content %}{{ key }}{% endwith %}{{ content }}', {'dict': {'key': 50}}, '50'),
     895            'with04': ('{{ content }}-{% with dict.key as key into content %}{{ key }}{% endwith %}-{{ content }}', {'dict': {'key': 50}}, '--50'),
    894896
    895897            'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
    896898            'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
     899            'with-error03': ('{% with dict.key as key into %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
     900            'with-error04': ('{% with dict.key as key blah content %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
    897901
    898902            ### NOW TAG ########################################################
    899903            # Simple case
Back to Top