twig tutorial - For Loop in Twig - twig for - for twig - twig loop - twig php - twig template
For Loop
- For loops can be really useful in TWIG, allowing the creation of data-dynamic web pages.
Say we create a simple array of numbers:
We can then iterate over the array and print out whatever we want. Any data within the array block will be outputted in relation to the amount of data within the array. This example would print out three h1 elements with the array data concatenated.
- Note that {{ current }} was used to access the data and not a version of array
A further example of this could be using objects. For example, say we have an Entity object with the field 'name' along with its relevant getters and setters. If a number of these entities are also stored within the Array, they can be accessed like any other Objects within TWIG: