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

Print image in blog template

Started by Joachim Kasper 9 years ago · 6 replies · 1274 views
9 years ago

HI Guys,

as a grav rookie i need some support.

i try to print a blog image in the blog_item.html.twig and need to add a class to it.

the default markup seems to be:

{% set header_image = page.header.header_image|defined(true) %}
{% set header_image_width = page.header.header_image_width|defined(680) %}
{% set header_image_height = page.header.header_image_height|defined(400) %}
{% set header_image_file = page.header.header_image_file %}

{% if header_image %}
{% if header_image_file %}
{% set header_image_media = page.media.images[header_image_file] %}
{% else %}
{% set header_image_media = page.media.images|first %}
{% endif %}
{{ header_image_media.cropZoom(header_image_width, header_image_height).html }}
{% endif %}

Can someone help me to get more familiar with the twig and how to get the image printed with an additional class in the <img> - tag?

9 years ago

i can't try it, because i didnt know how to render the url for the <img> from the the first header_image_media with a class.

9 years ago

@achikas:
images

see here in antimatter theme for instance
https://github.com/getgrav/grav-theme-antimatter/blob/develop/templates/blog.html.twig#L6-L12

it gets the first img from page media then you get the url when you add .url see example.

with the url you can do for instance
{% set media = '<img class="' ~ image_class ~ svg_classe ~'" src="' ~ image_media_url ~ '"' ~ image_width_attr ~ image_height_attr ~ image_alt ~ image_title ~ '/>' %}

NB: the code is just an example, you have to adapt it

9 years ago

thank you.

the missing output tag for the image url was {{ header_image_media.url }}

9 years ago

shortcut is:

TWIG
{{ header_image_media.cropZoom(header_image_width, header_image_height).classes('foo').html }}

notice the .classes('foo') part...

9 years ago

thanks for the shortcut. this gave me a real good support to start learning grav and twig!

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 43 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago