2

私は symfony を勉強していて、twig テンプレートに変数を表示したいと思っています。ここに私の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 }}

私はあなたの助けに感謝します、私の友達!

4

2 に答える 2

6

アクセスする前に、detailIndex キーが存在するかどうかを確認する必要があると思います。

Solution 1detailIndexターナーを避けるために、キーが存在しない場合は作成します

{% for row in issue.data %}

  {% if row.detailIndex is not defined %}
    {% set row.detailIndex = '' %}
  {% endif %}

... your business here

{% endfor %}

Solution 2ターナーを使用して値を取得しますdetailIndex。これは機能しますが、読みにくいです:)

{{ row.detailIndex is defined ? row.detailIndex : '' }}

Solution 3defaultフィルタを使用して、未定義の属性の例外を回避します

{{ row.detailIndex | default('') }}
于 2013-05-16T07:40:50.610 に答える
3

データは配列内にあるため、一方のメンバーにはキーdetailIndexがあり、もう一方のメンバーにはありません。してみてください

{{ row.detailIndex is defined ? row.detailIndex : '' }}

更新 1

別の試み

{{ if 'detailIndex' in row|keys ? row['detailIndex'] : '' }}
于 2013-05-16T04:44:35.570 に答える