This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Slasher
Slasher

I'm wondering if it's possible to sync PHP iterations with twig iterations.

I have this foreach loop in PHP:

foreach ($items as $img) {
      $this['picture'] = $img;
  }

And this for loop in Twig:

{% for image in gallery %}
   {{picture}} 
{% endfor %}

Now my output is the same, iteration count is correct (there are 3 picture in folder, but only getting last one):

  • img03.jpg
  • img03.jpg
  • img03.jpg

If I add echo in PHP:

foreach ($items as $img) {
     echo $this['picture'] = $img;
  }

Output is correct, but the content from echo is displayed above the HTML.

  • img01.jpg
  • img02.jpg
  • img03.jpg

It's possible to display correct output inside Twig loop?

mjauvin
mjauvin

Assign the array to a page variable and iterate through it in twig:

$this['images'] = $items;
{% for image in images %}
   {{ image }} 
{% endfor %}
Slasher
Slasher

@mjauvin Thank's a lot, problem solved!

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.