creative designer

Every now and again I see merchants creating alternate templates in their account that actually breaks some common Shopify code. The code assumes that if a template name contains the word 'product' that it must be a product template - that's not always the case.


The merchant could have in fact created an alternate page template called page.product.liquid. Since {{ template }} will see this as 'page.product' it is correctly matching the 'product' string and thinks "oh, this must be a product". This assumption means that the page may have script errors, or scripts firing when they shouldn't. Yuck.

The fix

Calling this a fix is a bit wrong, especially since it adds a little bit more server side processing. I'd prefer to take the hit on this, rather than surprise a merchant with a broken page.

To get the template just split the the template string at the dot, and return the first part. You'll get back things like 'page' or 'collection' or 'product'. eg:

{{ template | split:'.' | first }}

Now you could take this a little further if you like:

{% assign templateType = template | split:'.' %}
My template type is: {{ templateType[0] }}
{% if templateType[1] %}
My template alternate name is: {{ templateType[1] }}
{% endif %}

This means you could do something like so:

{% assign templateType = template | split:'.' %}
{% if templateType[0] == 'product' %}
I am - without a doubt - a product template
{% endif %}

And that's that. Not the cleanest method but it should work for you if you need it.

Like to work with me?

Let's talk