Twigのインストール
インストールします。twigはtemplateというレシピの中に入っています。
$ composer require template
インストール後の確認
git statusで何が追加されたの確認
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: composer.json
modified: composer.lock
modified: config/bundles.php
modified: symfony.lock
Untracked files:
(use "git add <file>..." to include in what will be committed)
config/packages/test/twig.yaml
config/packages/twig.yaml
templates/
git diffでconfig/bundles.phpの内容を確認
$ git diff config/bundles.php
diff --git a/config/bundles.php b/config/bundles.php
index 0d71512..a0be87b 100644
--- a/config/bundles.php
+++ b/config/bundles.php
@@ -3,4 +3,6 @@
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
+ Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
+ Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
];
テンプレートのレンダリングを作成する
src/Controller/QuestionController.php
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
// AbstractControllerを追記
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
// AbstractControllerを継承させる
class QuestionController extends AbstractController
{
/**
* @Route("/")
*/
public function homepage()
{
return new Response('Hello World');
}
/**
* @Route("/question/{slug}")
*/
public function show($slug)
{
// 変数をjson形式で入れる
$answers = [
'Make sure your cat is sitting purrrfectly still 🤣',
'Honestly, I like furry shoes better than MY cat',
'Maybe... try saying the spell backwards?',
];
// renderを追記することで、テンプレートの呼び出しが可能になる。
// renderの第一引数には、呼び出すテンプレートを、第二引数には、テンプレート内で使用する変数等を
return $this->render('question/show.html.twig', [
'question' => ucwords(str_replace('-', ' ', $slug)),
'answers' => $answers,
]);
}
}
テンプレートを作成する
呼び出せるレンダリングファイルを作成する
mkdir -p templates/question && touch templates/question/show.html.twig
ファイルの中を記述する
templates/question/show.html.twig
{# テンプレートは継承を使うことができる #}
{% extends 'base.html.twig' %}
{# titleブロックをオーバーライド #}
{% block title %}Question: {{ question }}{% endblock %}
{# bodyブロックをオーバーライド #}
{% block body %}
<h1>{{ question }}</h1>
<div>
Eventually, we`ll print the full question here!
</div>
<h2>Answers ({{ answers|length }})</h2>
<ul>
{% for answer in answers %}
<li>{{ answer }}</li>
{% endfor %}
</ul>
{% endblock %}
templates/base.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>
twigで使用される3つの構文
静的な呼び出し
{{ test }}
動的な呼び出し
{% for tests in test %}
コメント
{# コメント #}
参考
The Twig Recipe > Charming Development in Symfony 5 | SymfonyCasts
twigの公式ドキュメント、リファレンス
Home - Twig - The flexible, fast, and secure PHP template engine