Conversation
97eb3fb to
79f8a67
Compare
|
|
||
| .. code-block:: twig | ||
|
|
||
| {% set fruits = ['apple', 'orange', 'citrus'] %} |
There was a problem hiding this comment.
I like concrete examples instead of random ones. Let's revert to the fruits example.
There was a problem hiding this comment.
I keep this open: i'll rework it later today.
There was a problem hiding this comment.
No time nor inspiration these last day so ... back to fruits :)
|
Documentation states it "cycles over" .. and we see it behaves more as a "static modulo-accessor". I was wonderning if this function could return (or act as) an iterator ? |
|
@smnandre the common use case is to use this |
This is what i was thinking of .. would it be possible to not pass the "position" ? {% for turtle in turtles %}
🐢 {{ cycle([ '🔵', '🔴', '🟠', '🟣']) }}
{% endfor %}
{# would render #}
🐢 🔵 🐢 🔴 🐢 🟠 🐢 🟣 |
It already works: |
|
With Maybe this is something to highlight in the doc ? Or even deprecate the cycle function ? |
I think both serve different purposes. I forgot to say that this is available as of 4.0. Docs are available: https://github.com/twigphp/Twig/blob/4.x/doc/tags/for.rst |
00c2439 to
7c3c161
Compare
|
Thank you @smnandre. |
…cts (yoeunes)
This PR was merged into the 3.x branch.
Discussion
----------
Fix cycle() with non-countable ArrayAccess+Traversable objects
When using `cycle()` with an object that implements `\ArrayAccess` and `\Traversable` but is not `\Countable`, the function currently returns the object instance immediately after triggering the deprecation notice.
This prevents the value from being converted to an array, causing the cycle logic to fail or return the object itself.
This PR removes the early return to ensure `self::toArray()` is called, allowing these objects to be cycled correctly as expected.
**How to test**
```php
$seq = new class implements \ArrayAccess, \IteratorAggregate {
public function offsetExists($offset): bool { return true; }
public function offsetGet($offset): mixed { return 'val'; }
public function offsetSet($offset, $value): void {}
public function offsetUnset($offset): void {}
public function getIterator(): \Traversable { yield 'odd'; yield 'even'; }
};
// Should return 'odd', currently returns the $seq object
$result = CoreExtension::cycle($seq, 0);
```
Related to #4241
Commits
-------
473653d [Core] Fix cycle() with non-countable ArrayAccess+Traversable objects
Update code following #4158
Add doc precisions