如果语句不了解“真",则Shopify Liquid Theme提前自定义字段复选框.如实 [英] Shopify Liquid Theme advance custom field checkbox if-statement did not understand "true" as truthy

查看:82
本文介绍了如果语句不了解“真",则Shopify Liquid Theme提前自定义字段复选框.如实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过高级自定义字段插件在Shopify中的产品中添加了一些复选框.

I have added a few checkboxes to my products in Shopify via the Advanced Custom Fields plugin.

在主题中,我要遍历这些复选框,并且仅当该复选框被选中时,我才想在此处显示特定的翻译文本.

In the theme I would like to iterate through these checkboxes and only if the checkbox has been checked do I want to display a specific translation text here.

但是,我无法通过IF语句来检查该复选框是否被选中.

However, I fail on the IF statement to check whether the checkbox is checked or not.

这是我的代码(带有一些调试输出)

Here is my code (with some debugging output)

<ul>
  {% for field in product.metafields.warnhinweise %}
    {% assign field_first = field | first %}
    {% assign field_last = field | last %}
    <li>ID: {{ field_first }} - Value: {{ field_last }}</li>

    {% assign language_key = 'products.productwarnings.' | append: field_first %}

      {% if field_last == true %}
        <ul>
          <li>ID: {{ field_first }} - Value: {{ field_last }}</li>
          <ul>
            <li>nest the variable output: {{ language_key  | t }}</li>
          </ul>
        </ul>
      {% else %}
        <ul>
          <li>don't show</li>  
        </ul>
      {% endif %}
  {% endfor %}
</ul>

以下是输出:

结论:也许真实不真实吗?

Conclusion: Maybe true is not truthy?

我也尝试过此操作,以检查字符串:

I tried this as well, to check for a string:

{% if field_last == 'true' %}

但这会导致相同的结果.

But this leads to the same result.

这是高级自定义字段"后端的屏幕截图

And here is a screenshot from the Advanced Custom Fields backend

这是复选框的示例性配置.

Here is the exemplary configuration of a checkbox.

我真的想知道为什么这样一个简单的IF语句在这里行不通吗?

I really wonder why such a simple IF statement doesn't work here?

非常感谢Advance的帮助.

Thanks a lot in Advance for you help.

推荐答案

元字段可能未以与您的比较完全匹配的格式存储

当Shopify将值打印到文档中时,有时很难确定基础值的数据类型是什么.例如,如果将 true 打印到文档,是因为该值是布尔值 true ,所以字符串"true" ,包含 ['true'] 或其他内容的值的数组?

The metafields may not be being stored in a format that exactly matches your comparison

When Shopify prints values to your document, it can sometimes be hard to what the data type of the underlying value was. For example, if true is being printed to the document, is that because the value was the boolean true, the string "true", an array of values that contains ['true'], or something else?

要找出实际使用的确切值是多少,我建议使用 |.json 过滤器以输出变量.通过使用类似 {{field_last |json}} ,Shopify会将变量转换为javascript-legal格式,并将包含所有特殊字符(某些特殊字符(如引号)将被 \ 字符转义),从而揭示底层结构是什么.

To find out what the exact value being used under-the-hood is, I recommend using the | json filter to output your variable. By using something like {{ field_last | json }}, Shopify will convert the variable into a javascript-legal format and will include all special characters (and some special characters like quotation marks will be escaped by the \ character), thereby revealing what the underlying structure is.

在您的情况下,元字段值看起来像数组.建议不要在比较中为 contains 关键字切换等号运算符( == ):

In your case, it looks like the metafield value is array-like. Rather than going for an exact match on this construction, I would suggest switching the equality operator (==) in your comparison for the contains keyword:

{% if field_last contains 'true' %}
  <!-- Cool feature goes here! -->
{% endif %}

这篇关于如果语句不了解“真",则Shopify Liquid Theme提前自定义字段复选框.如实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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