mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 10:26:32 +00:00
ef7ade8ee3
* 3.x: (30 commits)
Make {} optional for the types tag
Remove the note about types mapping argument
Add a note on the types locality
Fix CS
Fix typos in the docs
Use spl_object_id() instead of spl_object_hash()
Add `LastModifiedExtensionInterface` and implementation in `AbstractExtension` to track modification of runtime classes
Bump version
Optimize NameExpression compilation
Fix typo in enum twig doc functions
Remove dead method in ModuleNode
Bump version
Prepare the 3.18.0 release
Add support for `Twig\Markup`
Fix unary operator precedence change
Replace Twigfiddle by the new Twig playground
Fix MacroNode $name argument can be overridden
Ignore exceptions from undefined handlers when using the guard tag
[Doc] Update the docs about attribute() function
Fix doc
...
75 lines
2.3 KiB
ReStructuredText
75 lines
2.3 KiB
ReStructuredText
Introduction
|
|
============
|
|
|
|
Welcome to the documentation for Twig, the flexible, fast, and secure template
|
|
engine for PHP.
|
|
|
|
Twig is 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 their own custom tags and filters, and to create their own DSL.
|
|
|
|
Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish,
|
|
phpBB, Matomo, OroCRM; and many frameworks have support for it as well like
|
|
Slim, Yii, Laravel, and Codeigniter — just to name a few.
|
|
|
|
.. admonition:: Screencast
|
|
|
|
Like to learn from video tutorials? Check out the `SymfonyCasts Twig Tutorial`_!
|
|
|
|
Prerequisites
|
|
-------------
|
|
|
|
Twig 4.x needs at least **PHP 8.2.0** to run.
|
|
|
|
Installation
|
|
------------
|
|
|
|
The recommended way to install Twig is via Composer:
|
|
|
|
.. code-block:: bash
|
|
|
|
composer require "twig/twig:^4.0"
|
|
|
|
Basic API Usage
|
|
---------------
|
|
|
|
This section gives you a brief introduction to the PHP API for Twig::
|
|
|
|
require_once '/path/to/vendor/autoload.php';
|
|
|
|
$loader = new \Twig\Loader\ArrayLoader([
|
|
'index' => 'Hello {{ name }}!',
|
|
]);
|
|
$twig = new \Twig\Environment($loader);
|
|
|
|
echo $twig->render('index', ['name' => 'Fabien']);
|
|
|
|
Twig uses a loader (``\Twig\Loader\ArrayLoader``) to locate templates, and an
|
|
environment (``\Twig\Environment``) to store its 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\FilesystemLoader('/path/to/templates');
|
|
$twig = new \Twig\Environment($loader, [
|
|
'cache' => '/path/to/compilation_cache',
|
|
]);
|
|
|
|
echo $twig->render('index.html.twig', ['name' => 'Fabien']);
|
|
|
|
.. _`SymfonyCasts Twig Tutorial`: https://symfonycasts.com/screencast/twig
|