felangel / felangel/mason

feat: access index in loops

Open
#419 5 comments 11 reactions 0 assignees View on GitHub
enhancement candidate
Dominant language
Dart
Stars
1.1k
Forks
113
PR merge metrics
No merged PRs in 30d

Description

**Description**

As a developer, there are many use-cases for knowing the current index and/or whether the current loop iteration is the first/last. For example, when generating a class definition and looping through parameters you may want to include commas in between parameters and a semicolon at the end. This is currently not intuitive/simple to accomplish because when using loops in mustache there's no built-in mechanism to query the current index.

**Proposal**

Provide variables to access the index within a loop (inspired by jinja)

```
{{#colors}}
The current color is {{.}}.
The current index is {{loop.index}}.
{{#loop.isFirst}}This is the first element in the array.{{/loop.isFirst}}{{#loop.isLast}}This is the last element in the array.{{/loop.isLast}}
{{/colors}}
```

Rendering the above template with `vars = { "colors": ["red, "green", "blue"] }` would result in:

```
The current color is red.
The current index is 0.
This is the first element in the array.

The current color is green.
The current index is 1.

The current color is blue.
The current index is 2.
This is the last element in the array.
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.