When you want to use some values of PHP in Twig templates,
_1. Create an associative array like below.
PHP
1$my_vals = array("id" => 3, "name" => "kuniatsu", "site" => "https://kusotail.com");
_2. Initialize Twig system and create an instance like below.
PHP
1// Register your template directory.
2// As a good news, you could specify multiple directories with an array.
3$loader = new Twig_Loader_Filesystem('/path/to/templates');
4
5// Initialize and create an instance of Twig as $twig.
6$twig = new Twig_Environment($loader, array(
7 'cache' => '/path/to/compilation_cache',
8));
_3. Then, render your template with an associative array that you created before. In this way, the associative array will be binded to values of Twig.
PHP
1echo $twig->render('index.html', $my_vals);
_4. So, you could use values as that key in your Twig template.
Twig
1<html>
2<body>
3{{ name }}のIDは{{ id }}です。
4<a href="{{ site }}">リンク</a>
5</body>
6</html>
Let's enjoy!