Skip to content

Commit 2dff721

Browse files
committed
Add else and loop explanation
1 parent db6f98d commit 2dff721

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,18 @@ Twig uses `for` to create loops, with a slightly different syntax compared to `f
108108
{% endforeach %}
109109
```
110110

111-
Internally it behaves the exact same way as `for`: it actually creates ForNode elements, so you have the same functionality like in `for` loops, including the `loop` variable.
111+
Internally it behaves the exact same way as `for`: it actually creates ForNode elements, so you have the same functionality like in `for` loops, including [the `loop` variable](https://twig.symfony.com/doc/3.x/tags/for.html#the-loop-variable) and `else`. `else` works the same as with `for`:
112+
113+
```twig
114+
{% foreach list as sublist %}
115+
{% foreach sublist as key => value %}
116+
{% else %}
117+
Array "sublist" is empty / no iteration took place
118+
{% endforeach %}
119+
{% else %}
120+
Array "list" is empty / no iteration took place
121+
{% endforeach %}
122+
```
112123

113124
### break and continue
114125

0 commit comments

Comments
 (0)