How we can use break statement in twig template?
Here is my php code
<?php
$i=0;
foreach($products as $product){
echo $product['name'];
$i++;
if($i==5){
break;
}
}
?>
How we can use break statement in twig template?
Here is my php code
<?php
$i=0;
foreach($products as $product){
echo $product['name'];
$i++;
if($i==5){
break;
}
}
?>
You either limit the array size inside you controller, which is the best method, or you could use the filter slice
{% for product in products|slice(0,5) %}
{{ product.name }}
{% endfor %}
Twig
doesn't include a break statement. According to the official manual, the equivalent instruction is the for + if condition
. Check the documentation here for some alternative.
or