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

Text.html.twig - different image on language

Started by Muut Archive 11 years ago · 3 replies · 352 views
11 years ago

Hello,
text.html.twig contains the reference to the first image:

TWIG
{% set image = page.media.images|first %}

is it possible to change it so that the image can change with the language extension?
Example: it should use image.en.png for English, image.it.png for Italian

11 years ago

page.media.images is just an array of all the images found in a particular folder. So getting |first will get whatever it finds first, irrespective of language.

If you want to get a particular language version of an image you'll have to employ some logic:

TWIG
 {{ set image = page.media.images['image.'~grav.language.getActive~'.png] }}
11 years ago

Thank you Andy, I had to change it a bit to make it work:

TWIG
{% set image = page.media.images['image.'~grav.language.getActive~'.png'] %}

to keep it meaningful (not just image.en.png), I decided to get the filename from an header parameter, writing .en / .itdirectly in the name:

TWIG

{% set image = page.media.images[page.header.image~'.png'] %}
---
11 years ago

Oh yah i had a missing single quote! sorry about that. Glad you got it sorted!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago