# Introduction
This is the documentation for Twig, the flexible, fast, and secure templateengine for PHP.
If you have any exposure to other text-based template languages, such asSmarty, Django, or Jinja, you should feel right at home with Twig. It's bothdesigner and developer friendly by sticking to PHP's principles and addingfunctionality useful for templating environments.
The key-features are...
- *Fast*: Twig compiles templates down to plain optimized PHP code. Theoverhead compared to regular PHP code was reduced to the very minimum.
- *Secure*: Twig has a sandbox mode to evaluate untrusted template code. Thisallows Twig to be used as a template language for applications where usersmay modify the template design.
- *Flexible*: Twig is powered by a flexible lexer and parser. This allows thedeveloper to define its own custom tags and filters, and create its own DSL.
Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish,phpBB, Piwik, OroCRM, and many frameworks have support for it as well likeSlim, Yii, Laravel, Codeigniter, and Kohana, just to name a few.
### Prerequisites
Twig needs at least **PHP 5.2.7** to run.
### Installation
The recommended way to install Twig is via Composer:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>composer require "twig/twig:~1.0"
</pre></div></td></tr></table>
Note
To learn more about the other installation methods, read the[*installation*](#) chapter; it also explains how to installthe Twig C extension.
### Basic API Usage
This section gives you a brief introduction to the PHP API for Twig.
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8</pre></div></td><td class="code"><div class="highlight"><pre>require_once '/path/to/vendor/autoload.php';
$loader = new Twig_Loader_Array(array(
'index' => 'Hello {{ name }}!',
));
$twig = new Twig_Environment($loader);
echo $twig->render('index', array('name' => 'Fabien'));
</pre></div></td></tr></table>
Twig uses a loader (`Twig_Loader_Array`) to locate templates, and anenvironment (`Twig_Environment`) to store the configuration.
The `render()` method loads the template passed as a first argument andrenders it with the variables passed as a second argument.
As templates are generally stored on the filesystem, Twig also comes with afilesystem loader:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre>$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
'cache' => '/path/to/compilation_cache',
));
echo $twig->render('index.html', array('name' => 'Fabien'));
</pre></div></td></tr></table>
Tip
If you are not using Composer, use the Twig built-in autoloader:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
</pre></div></td></tr></table>
- Twig
- Introduction
- Installation
- Twig for Template Designers
- Twig for Developers
- Extending Twig
- Twig Internals
- Deprecated Features
- Recipes
- Coding Standards
- Tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- Filters
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- Functions
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- Tests
- constant
- defined
- divisible by
- empty
- even
- iterable
- null
- odd
- same as