api - Shopify 液体 : How can I conditionally include snippets in Shopify liquid?

标签 api include code-snippets shopify liquid

我想在模板中包含一个片段,但前提是片段文件存在。有什么办法可以做到吗?

现在我只是使用:

{% include 'snippetName' %}

但这会引发错误:
Liquid error: Could not find asset snippets/snippetName.liquid

我需要这样一个功能的原因是因为我有一个后台进程稍后添加代码段。

最佳答案

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

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

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

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



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

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

关于api - Shopify 液体 : How can I conditionally include snippets in Shopify liquid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14667270/

相关文章:

javascript - 为什么每次运行 fetch 都会返回相同的结果?

c# - 在 VS2008 中用花括号 {} 包围代码块的任何方法?

python - Vim:如何安装 UltiSnips 插件?

c++ - 如何在 C++ 中创建单词/句子输出的随机机会

api - 如何使用 postman 模拟服务器

ios - 有没有办法通过 Apple 后端的一些 API 导出应用商店应用内支付?

api - Jhipster ssl api 身份验证 Ionic PWA 部署到 firebase

javascript - 如何在页脚中包含 jquery - 在页眉中使用 $(document).ready 时?

c++ - Doxygen 包含图

c - 尝试包含头文件时出现语法错误