Search code examples
javascripttemplatesswig-template

Swig Templates List Item Number


I can't figure out how you print the number of the list item using swig templates. Example( 1, 2, 3, 4, 5 ).

<ul>
  {% for result in results %}
  <li>
    <span>Item number: {{n}}</span>
    {{ result.title }}
  </li>
  {% endfor %}
</ul>

Solution

  • You can find the answer under the “Task #1” heading on this page:

    Specifically, within {% for %} tags in Swig, you have access to a variable called loop, which has an index property:

    <ul>
      {% for result in results %}
      <li>
        <span>Item number: {{ loop.index }}</span>
        {{ result.title }}
      </li>
      {% endfor %}
    </ul>
    

    See also the Swig docs: http://paularmstrong.github.io/swig/docs/tags/#for