Mat Som
Mat Som

Reputation: 120

How do i specify variable as alt attribute in a liquid HTML filter

Im trying to append the name of the variable in img alt attribute, from the official documentation, they're hard-code the alt text.

is there any working solution?

{% for product in products %}
  {{ product | img_tag: 'Picture of {{ product.name }}' }} 
{% endfor %}

Thank you

Upvotes: 0

Views: 559

Answers (1)

drip
drip

Reputation: 12933

You must do your logic outside and pass it as a variable.

{% for product in products %}
  {%- capture image_alt -%}Picture of {{ product.title }}{%- endcapture -%}
  {{ product | img_tag: image_alt }} 
{% endfor %}

And there is no product.name object but there is product.title.

Upvotes: 1

Related Questions