Is it only me who needs a simple tag with optional assignment, e.g. that 
both {% mytag %} and {% mytag as varname %} are possible?

Attached is a patch that make the assignment optional for assignment_tag.

If this change of behavior of assignment_tag is not acceptable, then how 
about adding a new decorator like 'optional_assignment_tag', or adding a 
new parameter to assignment_tag?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/kf4o1HEDJ3oJ.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Index: django/template/base.py
===================================================================
--- django/template/base.py	(revision 17915)
+++ django/template/base.py	(working copy)
@@ -1130,14 +1130,18 @@
             params, varargs, varkw, defaults = getargspec(func)
 
             class AssignmentNode(TagHelperNode):
-                def __init__(self, takes_context, args, kwargs, target_var):
+                def __init__(self, takes_context, args, kwargs, target_var=None):
                     super(AssignmentNode, self).__init__(takes_context, args, kwargs)
                     self.target_var = target_var
 
                 def render(self, context):
                     resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
-                    context[self.target_var] = func(*resolved_args, **resolved_kwargs)
-                    return ''
+                    output = func(*resolved_args, **resolved_kwargs)
+                    if self.target_var is None:
+                        return output
+                    else:
+                        context[self.target_var] = output
+                        return ''
 
             function_name = (name or
                 getattr(func, '_decorated_function', func).__name__)
@@ -1145,11 +1149,10 @@
             def compile_func(parser, token):
                 bits = token.split_contents()[1:]
                 if len(bits) < 2 or bits[-2] != 'as':
-                    raise TemplateSyntaxError(
-                        "'%s' tag takes at least 2 arguments and the "
-                        "second last argument must be 'as'" % function_name)
-                target_var = bits[-1]
-                bits = bits[:-2]
+                    target_var = None
+                else:
+                    target_var = bits[-1]
+                    bits = bits[:-2]
                 args, kwargs = parse_bits(parser, bits, params,
                     varargs, varkw, defaults, takes_context, function_name)
                 return AssignmentNode(takes_context, args, kwargs, target_var)

Reply via email to