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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

How can i add all images in a blog post using twig syntax?

Solved by pamtbaau View solution

Started by Takis 6 years ago · 4 replies · 1610 views
6 years ago

I was wondering if there is a way to add all images that belongs to that post using twig syntax in my blog_item.html.twig file. I know i could use markdown for this purpose like:

TXT
![](nature-1.jpg)

![](nature-2.jpg)

![](nature-3.jpg)

![](nature-4.jpg)

but this isn't an option for my project. I tried to add something like that:

TWIG
{% if page.media.images %}

{{page.media.images}}

{% endif %}

in my templates/partials/blog_item.html.twig but didn't work. What i m missing here? I searched almost all the grav documentation but cant find anything.

It would be very much appreciated if someone could give me some help. Thanks in advance.

6 years ago

@d3s1gn3r, To list all images in the folder of a page, try the following in your Twig template:

TWIG
{% for image in page.media.images %}
    {{ image.html }}
{% endfor %}
👍 1
6 years ago

Wow this was impressive and it works perfect. Thank you so much @pamtbaau. Just one more question. Is there any way to exclude the very first image from showing it in the post page ??

6 years ago Solution

@d3s1gn3r, See Twig documentation on the loop variable:

Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration
loop.last True if last iteration
loop.length The number of items in the sequence
loop.parent The parent context

Try:

TWIG
{% for image in page.media.images %}
    {% if not loop.first %}
        {{ image.html }}
    {% endif %}
{% endfor %}
👍 1
6 years ago

@pamtbaau once again your solution worked out of the box!! Thank you so much for your valuable help.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 82 12 hours ago
General · by pamtbaau, 17 hours ago
1 55 16 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 348 5 days ago
General · by Duc , 5 days ago
3 43 5 days ago