1Introduction
2============
3
4This is the documentation for Twig, the flexible, fast, and secure template
5engine for PHP.
6
7If you have any exposure to other text-based template languages, such as
8Smarty, Django, or Jinja, you should feel right at home with Twig. It's both
9designer and developer friendly by sticking to PHP's principles and adding
10functionality useful for templating environments.
11
12The key-features are...
13
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.
16
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.
20
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.
23
24Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish,
25phpBB, Piwik, OroCRM; and many frameworks have support for it as well like
26Slim, Yii, Laravel, Codeigniter and Kohana — just to name a few.
27
28Prerequisites
29-------------
30
31Twig needs at least **PHP 5.2.7** to run. As of 1.34, the minimum requirement
32was bumped to **PHP 5.3.3**.
33
34Installation
35------------
36
37The recommended way to install Twig is via Composer:
38
39.. code-block:: bash
40
41    composer require "twig/twig:~1.0"
42
43.. note::
44
45    To learn more about the other installation methods, read the
46    :doc:`installation<installation>` chapter; it also explains how to install
47    the Twig C extension.
48
49Basic API Usage
50---------------
51
52This section gives you a brief introduction to the PHP API for Twig.
53
54.. code-block:: php
55
56    require_once '/path/to/vendor/autoload.php';
57
58    $loader = new \Twig\Loader\ArrayLoader([
59        'index' => 'Hello {{ name }}!',
60    ]);
61    $twig = new \Twig\Environment($loader);
62
63    echo $twig->render('index', ['name' => 'Fabien']);
64
65Twig uses a loader (``\Twig\Loader\ArrayLoader``) to locate templates, and an
66environment (``\Twig\Environment``) to store the configuration.
67
68The ``render()`` method loads the template passed as a first argument and
69renders it with the variables passed as a second argument.
70
71As templates are generally stored on the filesystem, Twig also comes with a
72filesystem loader::
73
74    $loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
75    $twig = new \Twig\Environment($loader, [
76        'cache' => '/path/to/compilation_cache',
77    ]);
78
79    echo $twig->render('index.html', ['name' => 'Fabien']);
80
81.. tip::
82
83    If you are not using Composer, use the Twig built-in autoloader::
84
85        require_once '/path/to/lib/Twig/Autoloader.php';
86        Twig_Autoloader::register();
87