Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Looping through all Images in Page Folder

Solved by pamtbaau View solution

Started by NEmbgen 8 years ago · 2 replies · 2076 views
8 years ago

Hello,

I'm creating a highly specific template for a site.
It's a modular page. In one of the sub folders (lets call it _slider) are a variable amount of images.
How can I loop over all images and get their url (in the slider.html.twig) without knowing their names and number beforehand?

Cheers

8 years ago Solution

@nembgen Here is a small sample that might send you in the right direction.

Say you have a modular page with template 'templates/modular.html.twig', which loops through all child modules for the modular page:

TWIG
...
{% for module in page.collection() %}
    <div id="{{ _self.pageLinkName(module.menu) }}"></div>
    {{ module.content }}
{% endfor %}
...

And you have a folder like:

TXT
pages/
  01.home/
    _slider/
      image1.jpg
      image2.jpg
      ...
      slider.md
    _other_modules
      ...

Then the following template 'templates/modular/slider.html.twig', will loop through all images in folder '_slider':

TWIG
{% for image in page.media.images %}
    <img src="{{ image.url }}">
{% endfor %}
👍 1
8 years ago

Awesome! Thank you, it’s working.

Cheers

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 8 hours ago
1 2 12 minutes ago
General · by Andy Miller, 28 minutes ago
0 0 28 minutes ago
General · by Jerry Hunt, 4 days ago
2 136 1 day ago
General · by pamtbaau, 1 day ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago