I understand that I am responding to an old thread. However, I started using Jinja recently and ran into a similar problem: I needed to count the number of print lines in nested loops. My solution was to transfer the counter variable to the class and pass the instance to the template. Similarly, you can use a wrapper class like
class VoterStatus(object): def __init__(self, status='active'): self._status = status def set_disabled(self): self._status = 'disabled' def __str__(self): return self._status
Change your template accordingly
{% for voter in record.voters %} {% if user == voter %} {% status.set_disabled() %} {% endif %} {{ status }}
Pass an instance of the status class to the template for rendering:
tmplt.render(status=VoterStatus(), ...)
... and Bob is your uncle.
source share