I want to prevent using a loop twice in twig symfony3
If you prefer, you can build a string with the dynamic values (the lists of image tag) then use in the container div, as example:
{%set accumulator = '' %} {% for image in images %} {%set accumulator = accumulator ~ '<img src="/images/'~image.url~'"/>' %} {% endfor %}<div id="main-slider"> {{ accumulator|raw }}</div><div id="main-slider-nav"> {{ accumulator|raw }}</div>
Here a working twigfiddle sample
Hope this help