Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a list on the fly in a Django template

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.

like image 779
davidchambers Avatar asked Sep 15 '10 07:09

davidchambers


People also ask

How do I loop a list in a Django template?

The syntax of the Django template language involves four constructs: {{ }} and {% %} . In this shot, we will look at how to use a for loop in Django templates. The for template in Django is commonly used to loop through lists or dictionaries, making the item being looped over available in a context variable.

Can I use filter () in Django template?

Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.

What does {% mean in Django?

{% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {{ NAME }} this mean in Django templates?

8. What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.


1 Answers

If you want to add a new variable (ie some_list), you'll need access to the template's context, so simple_tag won'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:

@register.tag
def make_list(parser, token):
  bits = list(token.split_contents())
  if len(bits) >= 4 and bits[-2] == "as":
    varname = bits[-1]
    items = bits[1:-2]
    return MakeListNode(items, varname)
  else:
    raise template.TemplateSyntaxError("%r expected format is 'item [item ...] as varname'" % bits[0])

class MakeListNode(template.Node):
  def __init__(self, items, varname):
    self.items = map(template.Variable, items)
    self.varname = varname

  def render(self, context):
    context[self.varname] = [ i.resolve(context) for i in self.items ]
    return ""

And use it like this to create a new variable some_list:

{% make_list var1 var2 var3 as some_list %}

Feel free to give it a better name!

like image 157
Will Hardy Avatar answered Sep 28 '22 02:09

Will Hardy