Passing image size to url_filter as variable

Solved
Highlighted
Excursionist
19 5 6

Hello,

As the title suggests - I'm trying to use a snippet which will display image based on size determined as variable. My current code looks like this:

 

{% capture image_size %}
  {% if product_image_size %}
    {{ product_image_size }}
  {% else %}
    110x110
  {% endif %}
{% endcapture %}

{{ product.featured_image.src | img_url: image_size }}

 

product_image_size is obviously defined outside of snippet, and obviously url filter does not work unless I use plain text to specify image size. Why?

Thanks,
Lukasz 

0 Likes
Highlighted
Shopify Expert
507 12 98

This is an accepted solution.

When capturing your image_size value, you're also capturing whitespace and line breaks with your if statements and so you end up with an invalid value. Add whitespace control and it works.

{%- capture image_size -%}
  {%- if product_image_size -%}
    {{- product_image_size -}}
  {%- else -%}
    110x110
  {%- endif -%}
{%- endcapture -%}

{{ product.featured_image.src | img_url: image_size }}

 

0 Likes
Highlighted
Excursionist
19 5 6

Hi Gina,

Ah! As simple as that, thanks

0 Likes