a Sensio Labs Product

The flexible, fast, and secure
template language for PHP

Introduction

This is the documentation for Twig, the flexible, fast, and secure template language for PHP.

If you have any exposure to other text-based template languages, such as Smarty, Django, or Jinja, you should feel right at home with Twig. It's both designer and developer friendly by sticking to PHP's principles and adding functionality useful for templating environments.

The key-features are...

Prerequisites

Twig needs at least PHP 5.2.4 to run.

Installation

You have multiple ways to install Twig. If you are unsure what to do, go with the tarball.

From the tarball release

  1. Download the most recent tarball from the download page
  2. Unpack the tarball
  3. Move the files somewhere in your project

Installing the development version

  1. Install Subversion or Git
  2. For Subversion: svn co http://svn.twig-project.org/trunk/ twig, for Git: git clone git://github.com/fabpot/Twig.git

Installing the PEAR package

  1. Install PEAR
  2. pear channel-discover pear.twig-project.org
  3. pear install twig/Twig (or pear install twig/Twig-beta)

Basic API Usage

This section gives you a brief introduction to the PHP API for Twig.

The first step to use Twig is to register its autoloader:

require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

Replace the /path/to/lib/ path with the path you used for Twig installation.

Twig follows the PEAR convention names for its classes, which means you can easily integrate Twig classes loading in your own autoloader.

$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
 
$template = $twig->loadTemplate('Hello {{ name }}!');
 
$template->display(array('name' => 'Fabien'));

Twig uses a loader (Twig_Loader_String) to locate templates, and an environment (Twig_Environment) to store the configuration.

The loadTemplate() method uses the loader to locate and load the template and returns a template object (Twig_Template) which is suitable for rendering with the display() method.

Twig also comes with a filesystem loader:

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
  'cache' => '/path/to/compilation_cache',
));
 
$template = $twig->loadTemplate('index.html');
This website is powered by PHP and Twig. The Twig logo is © 2010 Sensio Labs

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.