0
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 3 years have passed since last update.

Twig よく使う書き方まとめ

Last updated at Posted at 2020-10-15

経緯

EC-CUBEを使用する案件でTwigを使用する機会があり、そもそもTwigとはなんぞや状態だったので、簡単にまとめてみました。

Twigとは?

Twigは簡素で読みやすいデザイナーにもフレンドリーなPHPテンプレート言語です。
以下、Twigでよく使う書き方の一例です。

コメントアウト
index.twig
{#コメント#}
変数の取得
index.twig
{{変数}}
配列の取得
index.twig
{{変数名,キー名}}
変数の作成
index.twig
{% set 変数名 =  %}
条件分岐処理
index.twig
{% if 条件式 %}
...
{% endif %}
繰り返し処理
index.twig
{% for 条件式 %}
...
{% endfor %}
値の出力
index.twig
{{dump(変数名など)}}
フィルター処理
index.twig
{{処理したいデータ[フィルター名]}}
ブロック処理
index.twig
{% block ブロック名 %}
...
{% endblock %}
継承処理
index.twig
{% extends 'ファイルパス' %}
挿入処理
index.twig
{% include 'ファイルパス' %}
0
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
0
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?