mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-24 09:08:38 +00:00
e870b1f341
* 1.x: deprecated _self added a base node visitor to make compat between 1.x and 2.x possible bumped to 1.20 added a mention of projects using Twig bumped version to 1.19.1-DEV prepared the 1.19.0 release updated CHANGELOG Fixed error guessing in blocks in a child template fixed CS tweaked docs fixed CS bumped version to 1.19 Added support for unconsumed macro arguments Added variadic filters, tests, and functions
79 lines
2.4 KiB
ReStructuredText
79 lines
2.4 KiB
ReStructuredText
Introduction
|
|
============
|
|
|
|
This is the documentation for Twig, the flexible, fast, and secure template
|
|
engine 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...
|
|
|
|
* *Fast*: Twig compiles templates down to plain optimized PHP code. The
|
|
overhead compared to regular PHP code was reduced to the very minimum.
|
|
|
|
* *Secure*: Twig has a sandbox mode to evaluate untrusted template code. This
|
|
allows Twig to be used as a template language for applications where users
|
|
may modify the template design.
|
|
|
|
* *Flexible*: Twig is powered by a flexible lexer and parser. This allows the
|
|
developer 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 like
|
|
Slim, Yii, Laravel, Codeigniter, and Kohana, just to name a few.
|
|
|
|
Prerequisites
|
|
-------------
|
|
|
|
Twig needs at least **PHP 5.5.0** to run.
|
|
|
|
Installation
|
|
------------
|
|
|
|
The recommended way to install Twig is via Composer:
|
|
|
|
.. code-block:: bash
|
|
|
|
composer require "twig/twig:~2.0"
|
|
|
|
.. note::
|
|
|
|
To learn more about the other installation methods, read the
|
|
:doc:`installation<installation>` chapter; it also explains how to install
|
|
the Twig C extension.
|
|
|
|
Basic API Usage
|
|
---------------
|
|
|
|
This section gives you a brief introduction to the PHP API for Twig.
|
|
|
|
.. code-block:: php
|
|
|
|
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'));
|
|
|
|
Twig uses a loader (``Twig_Loader_Array``) to locate templates, and an
|
|
environment (``Twig_Environment``) to store the configuration.
|
|
|
|
The ``render()`` method loads the template passed as a first argument and
|
|
renders it with the variables passed as a second argument.
|
|
|
|
As templates are generally stored on the filesystem, 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',
|
|
));
|
|
|
|
echo $twig->render('index.html', array('name' => 'Fabien'));
|