LoginSignup
9
6

More than 5 years have passed since last update.

[Twig] 動的なキーで配列要素を取得する

Last updated at Posted at 2016-03-10

やりたかったこと

こんなことがやりたかった。

$arr = array('key' => 'hoge');
$key = 'key';
print $arr[$key]; // hoge

結論

attribute を使う
http://twig.sensiolabs.org/doc/functions/attribute.html

ex
{% set arr = {'key': 'hoge'} %}
{% set key = 'key' %}
{{attribute(arr, key)}} // 'hoge'
9
6
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
9
6