I need a template variable to change by concatenating strings along the way.
I'm looping through a queryset in my template, and inside the loop I'm checking if certain fields on the object match the current user. If so, then my variable changes and is finally applied as a class to an element.
{% for item in items %}
{% with class="" %}
{% if request.user == item.field1 %}
/* append to class variable, ex: class=" field1" */
{% endif %}
{% if request.user == item.field2 %}
/* append to class variable, ex: class=" field1 field2" */
{% endif %}
<div class="{{class}}"></div>
{% endwith %}
{% endfor %}
So, if my request.user equals both item.field1 and item.field2 then my element would look like this:
<div class=" field1 field2"></div>
And if my request.user equals only item.field2 then my element would look like this:
<div class=" field2"></div>
You can't do that in Django template language. You could potentially write a tag, but I don't see why you would want to; you can just do the whole thing inline.
<div class="{% if request.user == item.field1 %}field1{% endif %} {% if request.user == item.field2 %}field2{% endif %}"></div>