Getting index in 'if' statement

Hi everyone,

I am adding a ‘for’ loop in tags in the template that iterates over a list of model objects. I would like it to have an ‘if’ statement asking if the object instance is the first in the list. How do I write this statement exactly? Something like this?

{% for model in model_list %}
     {% if model.index() == 0 %}

     {% endif %}
{% endfor %}

Thank you!

You’re looking for this part of the docs, specifically the parts around the forloop variable.

Hi @CodenameTim, Thanks for your reply! I can see there in that link the correct syntax for the ‘if’ statement. I still don’t see the how to access the model object index. Would it be something like this?

{% if model.index() == “0” %}

You don’t access the index into the model list - there really isn’t one to speak of. You access the counter for the loop.

As Tim pointed out:

1 Like

Hi KenWhitesell,

Thanks for your help! Yes, I can see now how a counter would work! Thanks to you both!