我有一個模態,它有使用這個數據數組的樹枝渲染的按鈕"buttons" => [ [ "title" => "Copy", "type" => "button", "attributes" => [ "data-action" => "confirm" ], "class" => "btn-primary", ], [ "title" => "Cancel", "type" => "button", "attributes" => [ "aria-label" => "Close" ], "class" => "btn-light", ]]如果已經有一個屬性為“aria-labal='Close'”的按鈕,我希望模式不在頂角顯示 [x],因此我添加了這組嵌套的 if 語句和 for 循環。{% set hideBtnClear = false %}{% for btn in modal.buttons %} {% if btn.attributes %} {% for key, value in btn.attributes %} {% if key == "aria-label" and value == "Close" %} {% set hideBtnClear = true %} {% endif %} {% endfor %} {% endif %}{% endfor %}{% if hideBtnClear == false %} [x] <--{% endif %}它有效但不是很優雅。有什么辦法可以改善它嗎?
2 回答

嗶嗶one
TA貢獻1854條經驗 獲得超8個贊
您也可以使用過濾器filter
來解決這個問題
{%?if?btns|filter(v?=>?v.attributes['aria-label']|default?==?'Close')?|?length?==?0?%} ????[?X?]? {%?endif?%}
使用not
代替== 0
也有效
{% if not btns|filter(v => v.attributes['aria-label']|default == 'Close') | length %}

白豬掌柜的
TA貢獻1893條經驗 獲得超10個贊
變化不大,但如果您知道 中所需的鍵btn.attributes,則只需檢查此鍵是否存在及其值:
{% set hideBtnClear = false %}
{% for btn in modal.buttons %}
{% if btn.attributes['aria-label'] is defined and btn.attributes['aria-label'] == "Close" %}
{% set hideBtnClear = true %}
{% endif %}
{% endfor %}
{% if hideBtnClear == false %}
[x] <--
{% endif %}
- 2 回答
- 0 關注
- 176 瀏覽
添加回答
舉報
0/150
提交
取消