6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Twigで配列のkeyを変数で指定

Posted at

変数の表示

Twigでは{{ 変数 }}のように変数を表示します。
配列でも{{ 配列.key }}または{{ 配列['key'] }}のように書きます。
それでは、keyに変数を指定したい場合はどうするのでしょうか?

配列のkeyを変数で指定

変数 = 'key'配列['key']を表示したいとします。
結論から言うと、{{ attribute(配列, 変数) }}で表示できます。
但し、これはTwigのver1.2で追加されたものなので気を付けて下さい。

これを応用して、配列1['key1'] = 'key2'配列2['key2']{{ attribute(配列2, 配列1.key1) }}とすることも出来ます。

終わりに

公式のHPのドキュメントにしっかりと書かれているけど、英語が苦手な私にはとてもキツイ(>_<)
英語しかないドキュメントが沢山あるから、何とかして読み解かないとと苦痛を味わう今日この頃です。

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?