Skip to content

Commit

Permalink
feat: add srcset for post image
Browse files Browse the repository at this point in the history
refactor: use picture element for responsive images
  • Loading branch information
rise-erpelding authored and jonoliver committed Oct 31, 2023
1 parent 6fb3bb0 commit 41163d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/php/views/page.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
{% block content %}
<div>
{% if post.thumbnail %}
<img
src="{{ post.thumbnail.src|resize(960) }}"
{% 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 41163d6

Please sign in to comment.