Shopify liquid: How can I conditionally include snippets in Shopify liquid?
Asked Answered
M

6

10

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.

Mileage answered 2/2, 2013 at 22:37 Comment(0)
B
24

Had this problem myself. This was my solution:

{% capture the_snippet_content %}{% include the_snippet %}{% endcapture %}
{% unless the_snippet_content contains "Liquid error" %}
  {% include reviews_snippet %}
{% endunless %}

Basically capture the snippet’s content as a variable. If there is no snippet Shopify generates the error:

Liquid error: Could not find asset snippets/caroline-flint-reviews.liquid

So check to see if it’s generated that… if so don’t print the snippet :D

Of course this would break if you intended your snippet to include "Liquid error" or if Shopify ever change the error message.

Byng answered 26/3, 2013 at 11:8 Comment(1)
So useful. Wound up using this to make a snippet routing system based off of handlesCairns
J
4

Extending on Jon's answer;

Create a file called snippet.liquid

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

Then when you want to include a file only if it exists

{% include 'snippet' with 'filename_of_include' %}
Jilolo answered 8/9, 2016 at 9:33 Comment(1)
Very neat implementation.Tenderize
C
4

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

Cockscomb answered 3/3, 2021 at 7:38 Comment(0)
C
1

Alternatively, you could create your own tag which does a check on the existence of the file, before attempting to process it.

https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#create-your-own-tags

Carping answered 11/7, 2013 at 3:54 Comment(1)
You can create your own tags if you manage your liquid from the 'roots'. Shopify has all the tags defined and you can't create new ones.Heterotopia
K
0

@vovafeldman Not sure why you can't have a blank snippet, but there's no file exists.

The only other option I can think of is since you are using a BG process to generate the snippet (and I assume upload it), you can always use the template API to upload the version of the template that includes the snippet at the same time.

Knitter answered 3/2, 2013 at 22:8 Comment(0)
T
0

Using the code listed above by Jon or a.wmly both still gave me errors. However, simply writing

{% include 'snippet_name' %}

worked just fine.

Note that this only worked for files located in the "snippets/" folder. So Templates, for instance, did not work using this method.

Takin answered 19/6, 2018 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.