LoginSignup
2
1

More than 5 years have passed since last update.

Symfony2.7のTwigでテンプレートを読み込む

Posted at

Twigで、{{ include("Page/list.html.twig")}}の様に書くことで、別のテンプレートをインポートできます。
コントローラーの$this->render第二引数に渡した値についてもそのまま渡してくれます。

includeのパスはapp/Resources/views/Page/information.html.twigの場合、Page/information.html.twigとなります。(app/Resources/viewsを省略する形)

information.html.twig
<h2>Information</h2>
<p>Informaiton is here</p>

これを読み込む場合は以下のように書きます。

base.html.twig
<html>
<body>
    <h1>{{ title }}</h1>
    {{ include("Page/information.html.twig")}}
</body>
</html>

これだけだと煩わしいだけですが、コンポーネントが増えていったり使いまわしたい要素が出てくると重宝します。

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