Shopify液体:我怎样才能有条件包括Shopify液体片段? [英] Shopify liquid: How can I conditionally include snippets in Shopify liquid?

查看:232
本文介绍了Shopify液体:我怎样才能有条件包括Shopify液体片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括在模板但前提是片段文件中存在的一个片段。有没有什么办法能做到吗?

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

但是,这引发错误:

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.

推荐答案

有这个问题我自己。这是我的解决方案:

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:

液体错误:找不到资产
  片段/卡罗琳·弗林特-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液体:我怎样才能有条件包括Shopify液体片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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