Last active January 15, 2013

Embed URL

HTTPS clone URL

SSH clone URL

Thinking this idea out to its fullest extent, brings up five possible forms:

Always output content, assignment via as varname is not allowed. The built
in {% now %} is an example.

Always output content, assignment via as varname is optional. The built in
{% cycle %} is an example.

Some times output, some times assign, depending upon the presence of
as varname. The built in {% url %} is an example.

Always assign, as varname is optional. There is no built in example.

Always assign, as varname is required. The built in {% regroup %} is an
example.

Currently, the template tag decorators for custom tags only support two forms:
form #1 via simple_tag and form #5 via assignment_tag. This proposal
is about extending the Django template tag decorators to expose the third and
fourth forms. A ticket exists for this proposal: Ticket #18651.

Rationale

The third form is already in use in the Django built in template tags - namely
the {% url %} tag. I have come across multiple situations similar to the
{% url %} tag where optional assignment would make things much simpler.
Consider the following use case:

Pros

New decorators are very easy to write. They do not have any surprising
behaviour, and they do exactly what they say on the tin.

Cons

It is a whole new decorator that has to be maintained, documented, tested, and
worked with. Most of the code will be duplicated between this decorator and the
assignment_tag decorator. This is not very DRY.

The names are overly verbose, but making them more concise will make then less
explicit.

Pros

Modifying the assignment_tag is a simple change. It involves two extra kwarg,
and some small modifications to the assignment_tag decorator. The assignment
tag is still used mostly for assignment. It prevents code duplication, and
keeps things DRY

Cons

The assignment_tag now does two things: assigning its result to a variable in
the context as normal, and outputting like simple_tag does. This is
surprising and inconsistent with the naming of the decorator.

The two new kwargs are mutually exclusive. Using them both at the same time
would be an error.

Pros

simple_tag can now act like some of the built in tags, like {% url %}. It
is easy to comprehend. It is less surprising than modifying assignment_tag to
not always assign things. It is very explicit in what it is doing.