twigphp / twigphp/Twig

Feature Request: Loop variable inside map filter

Open
#3,627 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
8.4k
Forks
1.3k
Avg merge
1d 13h
Merged PRs (30d)
37

Description

I really like that twig have a loop variable in its for loops.

It would be great to also have then in a loop with the the map filter. For example:

{% set options = (choices|map((choice) => {
   checked: (loop.first and value) and choice.value in value,
})) %}

https://twigfiddle.com/4ntrof

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the Twig map filter evaluates its callback and how the loop variable is provided in regular for loops. Check existing filter and loop-related tests before defining the expected behavior for the example; done means callbacks in map can access the appropriate loop state and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.