Check if a snippet exists

Highlighted
New Member
7 0 0

Hi,

I'm trying to check if a snippet exists. Can someone tell me how to do that, please.

 

Thanks,

Gavin

0 Likes
Shopify Expert
9778 86 1524

That's a question that needs more context. What's the use case? 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Expert
26 0 14

Something like this?

{% capture snippet_content %}{% include 'snippet' %}{% endcapture %}
{% unless snippet_check contains "Liquid error" %}
  {{ snippet_content }}
{% endunless %}

 

0 Likes
New Member
7 0 0

Thanks Dualsided, this is exactly what I'm after.

Is this the usual method to do this? I would have expected there to be a more direct approach with a dedicated liquid command/function to check for a snippet. But if this works then that's what I'll use.

Thanks again,

Gavin

0 Likes
Shopify Expert
9778 86 1524

It's not the usual method, but it's the only one if looking for a Liquid method. You'll see others doing similar things. The caveat here is if Shopify ever changes the error message, this will fail.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes