Code

Ticket #6378: 6378-into.diff

File 6378-into.diff, 3.8 KB (added by ericholscher, 5 years ago)

Initial patch that is a bit rough.

Line 
1Index: django/template/defaulttags.py
2===================================================================
3--- django/template/defaulttags.py      (revision 9904)
4+++ django/template/defaulttags.py      (working copy)
5@@ -416,10 +416,11 @@
6         return str(int(round(ratio)))
7
8 class WithNode(Node):
9-    def __init__(self, var, name, nodelist):
10+    def __init__(self, var, name, nodelist, into):
11         self.var = var
12         self.name = name
13         self.nodelist = nodelist
14+        self.into = into
15
16     def __repr__(self):
17         return "<WithNode>"
18@@ -430,7 +431,11 @@
19         context[self.name] = val
20         output = self.nodelist.render(context)
21         context.pop()
22-        return output
23+        if not self.into:
24+            return output
25+        else:
26+            context[self.into] = output
27+            return ''
28
29 #@register.tag
30 def autoescape(parser, token):
31@@ -1160,14 +1165,34 @@
32         {% with person.some_sql_method as total %}
33             {{ total }} object{{ total|pluralize }}
34         {% endwith %}
35+
36+        {% with person.some_sql_method as total into content %}
37+            {{ total }} object{{ total|pluralize }}
38+        {% endwith %}
39+        {{ content }}
40+
41     """
42     bits = list(token.split_contents())
43-    if len(bits) != 4 or bits[2] != "as":
44-        raise TemplateSyntaxError("%r expected format is 'value as name'" %
45+    if len(bits) == 4:
46+        if bits[2] != "as":
47+            raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" %
48                                   bits[0])
49+    elif len(bits) == 6:
50+        if bits[2] != "as" or bits[4] != "into":
51+            raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" %
52+                                  bits[0])
53+    else:
54+        raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" %
55+                                  bits[0])
56+
57     var = parser.compile_filter(bits[1])
58     name = bits[3]
59     nodelist = parser.parse(('endwith',))
60     parser.delete_first_token()
61-    return WithNode(var, name, nodelist)
62+    if len(bits) == 4:
63+        return WithNode(var, name, nodelist, '')
64+    elif len(bits) == 6:
65+        into = bits[5]
66+        return WithNode(var, name, nodelist, into)
67+
68 do_with = register.tag('with', do_with)
69Index: tests/regressiontests/templates/tests.py
70===================================================================
71--- tests/regressiontests/templates/tests.py    (revision 9904)
72+++ tests/regressiontests/templates/tests.py    (working copy)
73@@ -891,9 +891,13 @@
74             ### WITH TAG ########################################################
75             'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
76             'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')),
77+            'with03': ('{% with dict.key as key into content %}{{ key }}{% endwith %}{{ content }}', {'dict': {'key': 50}}, '50'),
78+            'with04': ('{{ content }}-{% with dict.key as key into content %}{{ key }}{% endwith %}-{{ content }}', {'dict': {'key': 50}}, '--50'),
79
80             'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
81             'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
82+            'with-error03': ('{% with dict.key as key into %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
83+            'with-error04': ('{% with dict.key as key blah content %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError),
84
85             ### NOW TAG ########################################################
86             # Simple case