#18651: Assignment tags should allow optional assignments ---------------------------------+-------------------------------------- Reporter: mitar | Owner: nobody Type: New feature | Status: closed Component: Template system | Version: 1.4 Severity: Normal | Resolution: needsinfo Keywords: | Triage Stage: Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 1 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 ---------------------------------+--------------------------------------
Comment (by tim_heap): I can think of two main use-cases for this, and two problems it solves. One of these use-cases has already been encountered in the built in tags, as documented below, so the problem it solves is real. Firstly, when a sensible default name exists for a tag, but you may want to change it. Consider a tag `{% get_comments_for object %}`, which find all comments against the object passed in, and assigns the result to the `comments` variable in the template. If you have two objects on the page, and want to get the comments from both, the names would collide. In this case, you could use `{% get_comments_for foo as foo_comments %}` and `{% get_comments_for bar as bar_comments %}`. The second use case is assigning or outputting the result. This is already used by the built in `{% url %}` tag. You can use `{% url foo %}` to print the URL directly, or `{% url foo as foo_url %}` to assign it to the `foo` variable in the template. This could be implemented as either two new decorators: {{{ @register.assignment_tag_with_default(default='comments') def get_comments_for(object): pass @register.optional_assignment_tag() def url(name, *args, **kwargs): pass }}} or as options on the existing decorator {{{ @register.assignment_tag(default_name='comments') def get_comments_for(object): pass @register.assignment_tag(optional_assignment=True) def url(name, *args, **kwargs): pass }}} In the second case of extending the current decorator, `default_name` and `optional_assignment` would have to be mutually exclusive. -- Ticket URL: <https://code.djangoproject.com/ticket/18651#comment:3> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.