Skip to content

Commit 41163d6

Browse files
rise-erpeldingjonoliver
authored andcommitted
feat: add srcset for post image
refactor: use picture element for responsive images
1 parent 6fb3bb0 commit 41163d6

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/php/views/page.twig

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
{% block content %}
44
<div>
55
{% if post.thumbnail %}
6-
<img
7-
src="{{ post.thumbnail.src|resize(960) }}"
8-
{% if post.thumbnail.alt and post.thumbnail.alt|length > 0 %}
9-
alt="{{ post.thumbnail.alt }}"
10-
{% endif %}
11-
/>
6+
{% set largest_image = post.thumbnail.src('large') %}
7+
<picture>
8+
{% for image_size in post.thumbnail.sizes|sort((a, b) => a.width <=> b.width)|keys %}
9+
{% set largest_image = post.thumbnail.src(image_size) %}
10+
<source srcset="{{ post.thumbnail.src(image_size) }}" media="(max-width: {{ post.thumbnail.sizes[image_size]['width'] }}px)" >
11+
{% endfor %}
12+
<img
13+
src="{{ largest_image }}"
14+
{% if post.thumbnail.alt and post.thumbnail.alt|length > 0 %}
15+
alt="{{ post.thumbnail.alt }}"
16+
{% else %}
17+
alt=""
18+
{% endif %}
19+
>
20+
</picture>
1221
{% endif %}
1322
{{ post.content }}
1423
{# Loads comments.php by default #}

src/php/views/partials/content-single.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class="post-preview__featured-image"
66
{% if post.thumbnail.alt and post.thumbnail.alt|length > 0 %}
77
alt="{{ post.thumbnail.alt }}"
8+
{% else %}
9+
alt=""
810
{% endif %}
911
/>
1012
{% endif %}

0 commit comments

Comments
 (0)