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.

Themes & Styling

Twig: Get image derivatives

Solved by pamtbaau View solution

Started by Jan Krötenstuhl 4 years ago · 2 replies · 487 views
4 years ago

Hello.

I'm relatively new to Grav and currently trying to work out the twig function .derivatives().

Is there a way to not "print" out the result but only use it for further processing?

I wanted to use the function inside a html <picture> element for it's <source>.

Thank you in advance!

4 years ago Solution

@kroetenstuhl,

You can call the Grav API in Twig to suit your needs. Eg.

TWIG
{% set derivatives = (page.media|first).derivatives(100, 1000, 100) %}

# The image with generated derivatives (listed in field 'alternatives')
{{ dump(derivatives)}}

# The final srcset string
{{ dump(derivatives.srcset)}}
👍 2
4 years ago

Thank you so much, @pamtbaau !

Now I feel a bit dumb, of course this is the way. 😅

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 198 2 months ago
Themes & Styling · by Ian, 2 months ago
3 94 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 456 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 48 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 128 3 months ago