I don’t know whether it’s possible, but I’d like to be able to write something like the following:
{% with var1 var2 var3 as some_list %}
{{ some_list|maximum }}
{% endwith %}
Creating a list on the fly from an arbitrary number of template variables and/or literals seems useful, so I’m hopeful that I’ve overlooked something simple.
Failing that, though, I’d like to know how to create a template tag which accepts an arbitrary number of arguments. (I’ve played around with simple_tag, which works well for tags which accept a fixed number of arguments.)
I don’t want to go to the trouble of creating a parser and subclassing django.template.Node until I’ve ascertained that there’s no simpler solution.
If you want to add a new variable (ie
some_list), you’ll need access to the template’s context, sosimple_tagwon’t be enough.For me, the first approach is to try to do this sort of work in the view, in order to keep the templates as simple as possible.
If that’s not appropriate, you’ll have to write the tag manually, like this:
And use it like this to create a new variable
some_list:Feel free to give it a better name!