Shopify Liquid:如何有条件地在 Shopify Liquid 中包含片段? [英] Shopify liquid: How can I conditionally include snippets in Shopify liquid?

查看:47
本文介绍了Shopify Liquid:如何有条件地在 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?

现在我只是在使用:

{% include 'snippetName' %}

但这会引发错误:

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.

推荐答案

我自己也遇到过这个问题.这是我的解决方案:

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 %}

基本上将代码段的内容捕获为变量.如果没有片段 Shopify 会生成错误:

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

液体错误:找不到资产片段/caroline-flint-reviews.liquid

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

所以检查它是否生成...如果是,请不要打印代码段:D

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

当然,如果您希望代码段包含液体错误"或 Shopify 曾经更改错误消息,这会中断.

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

这篇关于Shopify Liquid:如何有条件地在 Shopify Liquid 中包含片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆