This question already has an answer here:
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;
}
}
?>
</div>