Skip to content

Instantly share code, notes, and snippets.

@seandogg
Created June 7, 2024 21:51
Show Gist options
  • Save seandogg/382a397abf2e719bf1787b090c4bdfb5 to your computer and use it in GitHub Desktop.
Save seandogg/382a397abf2e719bf1787b090c4bdfb5 to your computer and use it in GitHub Desktop.
{%- comment -%}
Parameters
img_url: {string} Shopify image_url of the image including a width parameter eg. &width={width}
max_width: {number} maximum width used for srcset (optional)
{%- endcomment -%}
{%- liquid
assign widths = '160,320,480,640,800,960,1120,1280,1440,1600,1920,2240,2560' | split: ','
assign max_width_num = max_width | plus: 0
capture srcset_output
for width in widths
assign width_num = width | plus: 0
if max_width_num > 0 and max_width_num < width_num
break
endif
unless forloop.first
echo ', '
endunless
echo img_url | replace: '{width}', width | append: ' ' | append: width | append: 'w'
endfor
endcapture
echo srcset_output
-%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment