【问题标题】:TWIG syntax do not show field IFTWIG 语法不显示字段 IF
【发布时间】:2015-07-04 02:37:54
【问题描述】:

我有用 TWIG 编写的脚本。

Weight: {{ doc.weight }} kgs<br />

// 这部分脚本仅在“detail”和“info” MySQL 字段不为空时显示文本信息

                {% if "" == doc.info_hl %}
                    {% if '' != doc.detail_code %}
                        <b>Info: {{ doc.info }}</b>
                    {% endif %}
                {% else %}
                    Info: {{ doc.info_hl|raw }}
                {% endif %}
                <br />

                {% if "" == doc.detail_code_hl %}
                    {% if '' != doc.detail_code %}
                        <b>Details: {{ doc.detail_code }}</b>
                    {% endif %}
                {% else %}
                    <b>Details: {{ doc.detail_code_hl|raw }}</b>
                {% endif %}

有时 MySQL 字段“权重”的值为“0.00”

如何修改上面的代码 - DO NOT SHOW "weight" MySQL field IF value in this field "0.00" ? 我们可以在上面看到我们如何使用空文本字段来做到这一点,但是如何使用等于“0.00”的十进制字段来做到这一点?

提前感谢任何提示尝试!

【问题讨论】:

  • 我在您的代码中没有看到 weight。请提供相关代码块以提供更多上下文,并表现出为解决您的问题所做的真诚努力。
  • 重量:{{ doc.weight }} kgs
    - 在上面代码的开头!
  • @Ninir 你是说{% if "0.00" == doc.weight %} {% if '' != doc.weight %} Weight: {{ doc.weight }} kgs
  • 我的意思是{% if '0.00' == doc.weight %} Weight: {{ doc.weight }} kgs {% endif %}。根据您要检查的值,以上将适合您的需求。但是,Mike 说得对,如果要检查 doc.weight 不为空也不为 0,那么检查 {% if doc.weight is not empty %}Weight: {{ doc.weight }} kgs {% endif %} 会更好
  • @Ninir 它可以工作{% if '0.00' == doc.weight %} Weight: {{ doc.weight }} kgs {% endif %} 但反之亦然 - 它显示所有重量为“0.00”的位置,而不显示所有其他位置。它非常接近我正在寻找的解决方案 - 但我需要一些东西 {% if NOT '0.00' == doc.weight %} ...但我找不到这样的 TWIG 语法“如果不是”..

标签: php mysql twig


【解决方案1】:

简单写一下怎么样

{% if '0.00' != doc.weight %} Weight: {{ doc.weight }} kgs {% endif %}

甚至更好

{% if not ('0.00' == doc.weight) %}... {% endif %}

?

【讨论】:

    【解决方案2】:

    请查看Twig documentation 中的is empty 测试。此测试与 PHP empty() 函数的操作相同。这是一个方便的类型Comparison Table,可帮助您了解它会返回什么。

    如果预计此字段将只包含数字或数字的String 表示,您可以通过在变量中添加0 来绕过empty('0.00') === FALSE

    empty('0.00' + 0) === TRUE
    // In Twig: {%if ($value + 0) is empty %}
    

    这是 Twig 条件中可用的available Expressions 列表。

    【讨论】:

    • 我不确定这是正确的方向,因为字段 0.00 不为空
    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多