“onerror” Handler for PaaS (example Shopify)

The onerror handler can be handy when using PaaS’s like Shopify.

Here is an example that looks for a particular image file in the assets folder based on the product title, and offers a fallback in case it doesn’t exist:

{% if product.title %}
	{% capture new_file_name %}file-{{ product.title | handleize }}.gif{% endcapture %}
	<img src="{{ new_file_name | asset_url }}" onerror="this.src='{{ 'default.gif' | asset_url }}';" />
{% else %}
	<img src="{{ 'default.gif' | asset_url }}" />
{% endif %}

For more information about onerror see:

References: