dongping1012 2018-01-02 08:12
浏览 27
已采纳

树枝比较运算符表现不佳

Am trying to add a certain class to an anchor element if a given condition is met using symfony's twig templating engine, The following piece of code is being used in an attempt to achieve this:

{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
   <a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ (colorData[keys] is defined and colorData[keys]['code'] == c.hexcode) ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}

The above code adds the selected class rightfully, to just the first anchor element even when I expect to have 3 anchor elements assigned this class:

{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
   <a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ (colorData[keys] is defined and colorData[keys]['code'] in colors | keys) ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}

The second code fragment adds the selected class to 3 anchor elements because however you wish to look at it, colorData[keys]['code']has keys that exist in the colors array even if the class isn't being added to the right anchor elements. My question is this; if the comparison operator (==) returns true for matched variable values why isn't the first code fragment working? and why is the second code fragment adding this class to the wrong anchor elements?

A snapshot of the colorData array is shown below: enter image description here

That of the colors array is thus:

enter image description here

  • 写回答

2条回答 默认 最新

  • dongqiao1964 2018-01-02 10:04
    关注

    About the first example you're comparing:

    1. "#FFFFFF" == "#FFFFFF"
    2. "#222222" == "#795548"
    3. "#01579b" == "#3e2723"

    and after this comparsion it finishes 3 times in condition colorData[keys] is defined. What you can do here is to add one more loop.

    {% if colors is defined and colors is not empty %}
    {% for keys, c in colors %}
    <li>
       {% set isColorInColorData = false %}
       {% for exactColor in colorData %}
            {% if exactColor.code == c.hexcode %}
                 {% set isColorInColorData = true %}
            {% endif %}                
       {% endfor %}
    
       <a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ isColorInColorData ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
    </li>
    {% endfor %}
    {% endif %}
    

    Looking to the second example you're comparing a string with an integer.

    1. "#FFFFFF" in [0,1,2]
    2. "#222222" in [0,1,2]
    3. "#01579b" in [0,1,2]

    Note that "#FFFFFF" is "equal"(==) to 0 in php!!

    So you have to really concentrate on what are you comparing. In twig there is no === operator.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了