Skip to content

Commit

Permalink
refactor: use picture element for responsive images
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-jw committed Oct 30, 2023
1 parent 84221a1 commit 9943e98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/php/views/page.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
{% block content %}
<div>
{% if post.thumbnail %}
<img
src="{{ post.thumbnail.src('large') }}"
srcset="{{ post.thumbnail.src('thumbnail') }} 150w,
{{ post.thumbnail.src('medium') }} 300w,
{{ post.thumbnail.src('medium_large')}} 768w,
{{ post.thumbnail.src('large') }} 1024w,
{{ post.thumbnail.src|resize(960) }}"
sizes="(max-width: 960px) 100vw, 960px"
{% if post.thumbnail.alt and post.thumbnail.alt|length > 0 %}
alt="{{ post.thumbnail.alt }}"
{% endif %}
/>
{% set largest_image = post.thumbnail.src('large') %}
<picture>
{% for image_size in post.thumbnail.sizes|sort((a, b) => a.width <=> b.width)|keys %}
{% set largest_image = post.thumbnail.src(image_size) %}
<source srcset="{{ post.thumbnail.src(image_size) }}" media="(max-width: {{ post.thumbnail.sizes[image_size]['width'] }}px)" >
{% endfor %}
<img
src="{{ largest_image }}"
{% if post.thumbnail.alt and post.thumbnail.alt|length > 0 %}
alt="{{ post.thumbnail.alt }}"
{% else %}
alt=""
{% endif %}
>
</picture>
{% endif %}
{{ post.content }}
{# Loads comments.php by default #}
Expand Down
2 changes: 2 additions & 0 deletions src/php/views/partials/content-single.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
class="post-preview__featured-image"
{% if post.thumbnail.alt and post.thumbnail.alt|length > 0 %}
alt="{{ post.thumbnail.alt }}"
{% else %}
alt=""
{% endif %}
/>
{% endif %}
Expand Down

0 comments on commit 9943e98

Please sign in to comment.