2
1

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で変数が未定義の場合に初期化する

Posted at

気づけば簡単ですがはまったのでメモ

これではhoge変数は初期化されない

 {% set hoge|default(false) %}

これで初期化される

 {% set hoge = hoge|default(false) %}

複数のControllerから呼ばれるようなtwigファイルの場合、
すべてのControllerにいちいち定義を書くのがめんどくさかったのでtwig上でなんとかしようとした結果。

ただこの書き方をしているとtwigが汚くなるし、デザイナーの人などが見たときにわかりにくくなる可能性が高いので
乱発は控えたほうがよさそう。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?