Skip to content

[Feature Request] Loop-related variables #65

@cossssmin

Description

@cossssmin

Would you accept a PR that adds a loop object to the locals, when inside a loop?

It would expose the following object:

loop: {
  index: 0,
  remaining: 2,
  first: true,
  last: false,
  length: 3
}

So you could use it inside a loop, like this:

<each loop='value, key in items'>
  <tr class="{{ loop.index % 2 == 0  ? 'even' : 'odd' }}">
    <td>{{ key }}: {{ value }} {{ loop.first ? 'is first' : '' }}</td>
    
    <if condition="loop.last">
      <td class="last"></td>
    </if>
  </tr>
</each>

Of course, it would be available when looping over both arrays and objects.

Could be useful for various things, but I've frequently used it for:

  • outputting content based on where we are in the loop
  • building paginations

This is inspired by the for loop in Nunjucks.

I already have it working locally, so if this is something you also find useful, I can do a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions