4 This is the documentation for Twig, the flexible, fast, and secure template
7 If you have any exposure to other text-based template languages, such as
8 Smarty, Django, or Jinja, you should feel right at home with Twig. It's both
9 designer and developer friendly by sticking to PHP's principles and adding
10 functionality useful for templating environments.
12 The key-features are...
14 * *Fast*: Twig compiles templates down to plain optimized PHP code. The
15 overhead compared to regular PHP code was reduced to the very minimum.
17 * *Secure*: Twig has a sandbox mode to evaluate untrusted template code. This
18 allows Twig to be used as a template language for applications where users
19 may modify the template design.
21 * *Flexible*: Twig is powered by a flexible lexer and parser. This allows the
22 developer to define their own custom tags and filters, and to create their own DSL.
24 Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish,
25 phpBB, Piwik, OroCRM; and many frameworks have support for it as well like
26 Slim, Yii, Laravel, Codeigniter and Kohana — just to name a few.
31 Twig needs at least **PHP 5.2.7** to run. As of 1.34, the minimum requirement
32 was bumped to **PHP 5.3.3**.
37 The recommended way to install Twig is via Composer:
41 composer require "twig/twig:~1.0"
45 To learn more about the other installation methods, read the
46 :doc:`installation<installation>` chapter; it also explains how to install
52 This section gives you a brief introduction to the PHP API for Twig.
56 require_once '/path/to/vendor/autoload.php';
58 $loader = new Twig_Loader_Array(array(
59 'index' => 'Hello {{ name }}!',
61 $twig = new Twig_Environment($loader);
63 echo $twig->render('index', array('name' => 'Fabien'));
65 Twig uses a loader (``Twig_Loader_Array``) to locate templates, and an
66 environment (``Twig_Environment``) to store the configuration.
68 The ``render()`` method loads the template passed as a first argument and
69 renders it with the variables passed as a second argument.
71 As templates are generally stored on the filesystem, Twig also comes with a
74 $loader = new Twig_Loader_Filesystem('/path/to/templates');
75 $twig = new Twig_Environment($loader, array(
76 'cache' => '/path/to/compilation_cache',
79 echo $twig->render('index.html', array('name' => 'Fabien'));
83 If you are not using Composer, use the Twig built-in autoloader::
85 require_once '/path/to/lib/Twig/Autoloader.php';
86 Twig_Autoloader::register();