Created
June 19, 2019 09:24
-
-
Save piotrpog/1c930a51f7f2ecc6d2cce8bbdbf7b052 to your computer and use it in GitHub Desktop.
Responsive image macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro responsiveImage(file, sizes, options) %} | |
{# settings #} | |
{% set pixel = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==' %} | |
{# logic #} | |
{% if file is defined and sizes is defined and sizes is iterable %} | |
{% if sizes|first is iterable or sizes|first is null %} | |
{# array of transforms #} | |
<picture class="{{options.class ?? null}}"> | |
{% for media, settings in sizes %} | |
{% set url = settings is not null ? file.getUrl(settings) : pixel %} | |
<source media="{{media}}" srcset="{{url}}"></source> | |
{% endfor %} | |
{% set urlFallback = sizes|first is not null ? file.getUrl(sizes|first) : pixel %} | |
<img src="{{urlFallback}}" alt="{{options.alt ?? null}}"> | |
</picture> | |
{% else %} | |
{# single transform #} | |
<img src="{{file.getUrl(sizes)}}" alt="{{options.alt ?? null}}" class="{{options.class ?? null}}"> | |
{% endif %} | |
{% endif %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment