twigHow to use the 'for' loop with PHP and Twig?
The for
loop is a powerful tool for iterating over a set of data in PHP and Twig. It allows you to execute a block of code for each item in a set of data.
Example code
{% for item in items %}
<p>{{ item }}</p>
{% endfor %}
Output example
<p>Item 1</p>
<p>Item 2</p>
<p>Item 3</p>
Code explanation
for
: the keyword used to start the loopitem
: the variable used to store the current item in the loopitems
: the set of data being looped overendfor
: the keyword used to end the loop
Helpful links
More of Twig
- How to use a PHP function in Twig?
- How to check if a string contains a substring in PHP Twig?
- How to embed YouTube videos in Twig with PHP?
- How to parse XLSX in Twig with PHP?
- How to include a Twig file with PHP?
- How to handle whitespace in Twig with PHP 7.4?
- How to use a Twig file in PHP?
- How to prevent Template Injection in PHP Twig?
- How to get a substring in PHP Twig?
- How to set a session variable in PHP Twig?
See more codes...