我正在研究symfony,并希望在我的小树枝模板中显示一个变量。这是我的PHP代码:
{% for row in issue.data %}
{{ dump(row) }}
我在我的网站上得到的是:
array(size=5)
'positionId' =>int5
'position' =>int5
'cost' =>float3000
'detailIndex' =>int1
'hours' =>int3
所以我用的是:
{{ row.detailIndex }}
访问我的数组变量,但出现错误:
Item "detailIndex" for "Array" does not exist in
这很奇怪,因为我可以很容易地访问这些变量:
{{ row.hours }}
{{ row.position }}
{{ row.cost }}
我会感谢你的任何帮助,我的朋友们!
我认为您应该在访问detailIndex密钥之前检查它是否存在。
解决方案1如果detailIndex
密钥不存在,则创建该密钥以避免terners
{% for row in issue.data %}
{% if row.detailIndex is not defined %}
{% set row.detailIndex = '' %}
{% endif %}
... your business here
{% endfor %}
解决方案2使用terner获取detailIndex
值。这是有效的,但读起来不太好:(
{{ row.detailIndex is defined ? row.detailIndex : '' }}
解决方案3使用default
过滤器避免未定义的属性异常
{{ row.detailIndex | default('') }}
由于数据在数组中,可能一个成员有密钥detailIndex
,而另一个没有。尝试
{{ row.detailIndex is defined ? row.detailIndex : '' }}
更新1
再次尝试
{{ if 'detailIndex' in row|keys ? row['detailIndex'] : '' }}