Welcome to the documentation for Twig, the flexible, fast, and secure template engine for PHP.
Twig is both designer and developer friendly by sticking to PHP’s principles and adding functionality useful for templating environments.
The key-features are…
Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish, phpBB, Matomo, OroCRM; and many frameworks have support for it as well like Slim, Yii, Laravel, and Codeigniter — just to name a few.
Twig 3.x needs at least PHP 7.2.5 to run.
The recommended way to install Twig is via Composer:
composer require "twig/twig:^3.0"
This section gives you a brief introduction to the PHP API for Twig:
require_once '/path/to/vendor/autoload.php';
$loader = new \Twig\Loader\ArrayLoader([
'index' => 'Hello {{ name }}!',
]);
$twig = new \Twig\Environment($loader);
echo $twig->render('index', ['name' => 'Fabien']);
Twig uses a loader (\Twig\Loader\ArrayLoader) to locate templates, and an environment (\Twig\Environment) to store its configuration.
The render() method loads the template passed as a first argument and renders it with the variables passed as a second argument.
As templates are generally stored on the filesystem, Twig also comes with a filesystem loader:
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, [
'cache' => '/path/to/compilation_cache',
]);
echo $twig->render('index.html', ['name' => 'Fabien']);
© 2009–2018 by the Twig Team
Licensed under the three clause BSD license.
The Twig logo is © 2010–2020 Symfony
https://twig.symfony.com/doc/3.x/intro.html