Skip to content

Instantly share code, notes, and snippets.

@M-Drummond
Last active March 3, 2024 09:58
Show Gist options
  • Save M-Drummond/614e1306dcebfa8cec67e13a3fe73a0f to your computer and use it in GitHub Desktop.
Save M-Drummond/614e1306dcebfa8cec67e13a3fe73a0f to your computer and use it in GitHub Desktop.
CraftCMS - get the webp/jpeg version of a a sourceset.
{% macro imageSourceTags( image ) %}
{% if image.extension is defined %}
{% if image | length >= 1 and image.extension != 'svg' %}
{% set formats = ['webp', 'jpg'] %}
{% for format in formats %}
{% set srcset = image.getSrcset([185, 576, 768, 960, 1454, 1920, 2560], { format: format }) %}
<source srcset="{{ srcset }}" type="image/{{ format }}" sizes="100vw">
{% endfor %}
{% endif %}
{% endif %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment