Skip to content

Add loop.changed, loop.previous, loop.next, and loop.cycle variables#4135

Merged
fabpot merged 2 commits into
twigphp:4.xfrom
fabpot:loop-more-features
Jul 12, 2024
Merged

Add loop.changed, loop.previous, loop.next, and loop.cycle variables#4135
fabpot merged 2 commits into
twigphp:4.xfrom
fabpot:loop-more-features

Conversation

@fabpot

@fabpot fabpot commented Jul 12, 2024

Copy link
Copy Markdown
Contributor

No description provided.

@fabpot fabpot force-pushed the loop-more-features branch from b92496e to 65b4ea4 Compare July 12, 2024 09:13
Comment thread doc/tags/for.rst
{{ loop.index }} - {{ user.username }}
{% endfor %}

Use ``loop.cycle`` to cycle among a list of values:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add this one give we already implemented the standalone cycle filter (that was added later in Jinja) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shortcut. Maybe it's not needed, but I find it nice to not have to care about the second arg.

Comment thread doc/tags/for.rst Outdated
Comment thread doc/tags/for.rst Outdated
Comment thread src/Runtime/LoopIterator.php Outdated
Comment thread doc/tags/for.rst
@@ -55,9 +55,9 @@ The ``loop`` variable

Inside of a ``for`` loop block you can access some special variables:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describing loop.changed as a variable looks weird to me, as changed is not a getter but a method requiring an argument.

Anyway, from a strict point of view, there is only 1 special variable: loop. What this table describes is the API of this loop variable.

@fabpot fabpot force-pushed the loop-more-features branch from 34dfb04 to 55b414c Compare July 12, 2024 16:06
@fabpot fabpot merged commit dc00598 into twigphp:4.x Jul 12, 2024
@fabpot fabpot deleted the loop-more-features branch July 12, 2024 17:18
Comment thread doc/tags/for.rst
``loop.cycle`` Cycle over a sequence of values
``loop.changed`` True if previously called with a different value or if not called yet
``loop.previous`` The value from the previous iteration (``null`` for the first iteration)
``loop.next`` The value from the next iteration (``null`` for the first iteration)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-``loop.next``         The value from the next iteration (``null`` for the first iteration)
+``loop.next``         The value from the next iteration (``null`` for the last iteration)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has already been fixed in #4142 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants