a Symfony Product

The flexible, fast, and secure
template engine for PHP

You are reading the documentation for Twig 1.x. Switch to the documentation for Twig 2.x.

Questions & Feedback


Twig documentation is licensed under the new BSD license.


You have multiple ways to install Twig.

Installing the Twig PHP package

Installing from the tarball release

  1. Download the most recent tarball from the download page
  2. Verify the integrity of the tarball http://fabien.potencier.org/article/73/signing-project-releases
  3. Unpack the tarball
  4. Move the files somewhere in your project

Installing the development version

git clone git://github.com/twigphp/Twig.git

Installing the PEAR package


Using PEAR for installing Twig is deprecated and Twig 1.15.1 was the last version published on the PEAR channel; use Composer instead.

pear channel-discover pear.twig-project.org
pear install twig/Twig

Installing the C extension

New in version 1.4: The C extension was added in Twig 1.4.


The C extension is optional but it brings some nice performance improvements. Note that the extension is not a replacement for the PHP code; it only implements a small part of the PHP code to improve the performance at runtime; you must still install the regular PHP code. The C extension is only compatible and useful for PHP5.

Twig comes with a C extension that enhances the performance of the Twig runtime engine; install it like any other PHP extensions:

cd ext/twig
make install


You can also install the C extension via PEAR (note that this method is deprecated and newer versions of Twig are not available on the PEAR channel):

pear channel-discover pear.twig-project.org
pear install twig/CTwig

For Windows:

  1. Setup the build environment following the PHP documentation
  2. Put Twig's C extension source code into C:\php-sdk\phpdev\vcXX\x86\php-source-directory\ext\twig
  3. Use the configure --disable-all --enable-cli --enable-twig=shared command instead of step 14
  4. nmake
  5. Copy the C:\php-sdk\phpdev\vcXX\x86\php-source-directory\Release_TS\php_twig.dll file to your PHP setup.


For Windows ZendServer, ZTS is not enabled as mentioned in Zend Server FAQ.

You have to use configure --disable-all --disable-zts --enable-cli --enable-twig=shared to be able to build the twig C extension for ZendServer.

The built DLL will be available in C:\\php-sdk\\phpdev\\vcXX\\x86\\php-source-directory\\Release

Finally, enable the extension in your php.ini configuration file:

extension=twig.so #For Unix systems
extension=php_twig.dll #For Windows systems

And from now on, Twig will automatically compile your templates to take advantage of the C extension. Note that this extension does not replace the PHP code but only provides an optimized version of the \Twig\Template::getAttribute() method.

Website powered by Symfony and Twig, deployed on
The Twig logo is © 2010-2019 Symfony