Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shopify liquid: How can I conditionally include snippets in Shopify liquid?

I would like to include a snippet in a template but only if the snippet file exist. Is there any way I can do it?

Now I'm just using:

{% include 'snippetName' %}

But this throws the error:

Liquid error: Could not find asset snippets/snippetName.liquid

The reason I need such a functionality is because I have a background process that adds the snippet later on.

like image 549
vovafeldman Avatar asked Feb 02 '13 22:02

vovafeldman


1 Answers

Okay, Coming here in 2021.

The include syntax is deprecated and infrequently used, also extending @a.wmly answer, this should be the latest syntax replacing include with render:

{% capture snippet_content %}{% render 'your-snippet-name' %}{% endcapture %}
{% if snippet_content contains "Could not find asset" %}
    {% comment %} do nothing {% endcomment %}
{% else %}
    {% render 'your-snippet-name' %}
{% endif %}

references for include vs render : https://shopify.dev/docs/themes/liquid/reference/tags/deprecated-tags#include

like image 106
Rohit Gupta Avatar answered Sep 28 '22 06:09

Rohit Gupta