- Twig
- The flexible, fast, and secure template engine for PHP
- Table of Contents
- Questions & Feedback
- License
- Introduction
- Prerequisites
- Installation
- Basic API Usage
- Twig
- The flexible, fast, and secure template engine for PHP
- Table of Contents
- Questions & Feedback
- License
- Introduction
- Prerequisites
- Installation
- Basic API Usage
- Twig
- The flexible, fast, and secure template engine for PHP
- Twig is a modern template engine for PHP
- What makes Twig better than PHP as a template engine?
- Twig
- The flexible, fast, and secure template engine for PHP
- Table of Contents
- Questions & Feedback
- License
- Introduction
- Prerequisites
- Installation
- Basic API Usage
- Twig
- The flexible, fast, and secure template engine for PHP
- Questions & Feedback
- License
- Twig
Twig
The flexible, fast, and secure
template engine for PHP
Table of Contents
Questions & Feedback
Found a typo or an error?
Want to improve this document? Edit it.
Need support or have a technical question?
Ask support on Stack Overflow.
License
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.
- 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.
Like to learn from video tutorials? Check out the SymfonyCasts Twig Tutorial!
Prerequisites
Twig 2.x needs at least PHP 7.2.5 to run.
Installation
The recommended way to install Twig is via Composer:
composer require "twig/twig:^2.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 >!', ]); $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', ['name' => 'Fabien']);
Twig
The flexible, fast, and secure
template engine for PHP
Table of Contents
Questions & Feedback
Found a typo or an error?
Want to improve this document? Edit it.
Need support or have a technical question?
Ask support on Stack Overflow.
License
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.
- 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.
Like to learn from video tutorials? Check out the SymfonyCasts Twig Tutorial!
Prerequisites
Twig 3.x needs at least PHP 7.2.5 to run.
Installation
The recommended way to install Twig is via Composer:
composer require "twig/twig:^3.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 >!', ]); $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', ['name' => 'Fabien']);
Twig
The flexible, fast, and secure
template engine for PHP
Twig is a modern template engine for PHP
- 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.
What makes Twig better than PHP as a template engine?
When it comes to template engines in PHP, many people will tell you that PHP itself is a template engine. But even if PHP started its life as a template language, it did not evolve like one in the recent years. As a matter of fact, it doesn’t support many features modern template engines should have nowadays:
- Concise: The PHP language is verbose and becomes ridiculously verbose when it comes to output escaping:
echo $var ?> echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>
for user in users %> * > else %> No users have been found. endfor %>
extends "layout.html" %> block content %> Content of the page. endblock %>
Of course, PHP is also the language for which you can find the more template engine projects. But most of them do not embrace web development best practices yet:
- Extensibility: Twig is flexible enough for all your needs, even the most complex ones. Thanks to an open architecture, you can implement your own language constructs (tags, filters, functions, and even operators) to create your very own DSL.
- Unit tested: Twig is fully unit-tested. The library is stable and ready to be used in large projects.
- Documented: Twig is fully documented, with a dedicated online book, and of course a full API documentation.
- Secure: When it comes to security, Twig has some unique features:
- Automatic output escaping: To be on the safe side, you can enable automatic output escaping globally or for a block of code:
autoescape "html" %> > raw >> escape >> endautoescape %>
Twig
The flexible, fast, and secure
template engine for PHPTable of Contents
Questions & Feedback
Found a typo or an error?
Want to improve this document? Edit it.Need support or have a technical question?
Ask support on Stack Overflow.License
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.
- 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.
Like to learn from video tutorials? Check out the SymfonyCasts Twig Tutorial!
Prerequisites
Twig 1.x needs at least PHP 7.2.5 to run.
Installation
The recommended way to install Twig is via Composer:
composer require "twig/twig:^1.0"
To learn more about the other installation methods, read the 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:
require_once '/path/to/vendor/autoload.php'; $loader = new \Twig\Loader\ArrayLoader([ 'index' => 'Hello >!', ]); $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', ['name' => 'Fabien']);
Twig
The flexible, fast, and secure
template engine for PHPQuestions & Feedback
Found a typo or an error?
Want to improve this document? Edit it.Need support or have a technical question?
Ask support on Stack Overflow.License
Twig
- Introduction
- Prerequisites
- Installation
- Basic API Usage
- Synopsis
- IDEs Integration
- Variables
- Filters
- Functions
- Named Arguments
- Control Structure
- Comments
- Including other Templates
- Template Inheritance
- HTML Escaping
- Escaping
- Macros
- Expressions
- Whitespace Control
- Extensions
- Basics
- Rendering Templates
- Environment Options
- Loaders
- Using Extensions
- Built-in Extensions
- Exceptions
- Globals
- Filters
- Functions
- Tests
- Tags
- Creating an Extension
- Testing an Extension
- How does Twig work?
- The Lexer
- The Parser
- The Compiler
- Displaying Deprecation Notices
- Making a Layout conditional
- Making an Include dynamic
- Overriding a Template that also extends itself
- Customizing the Syntax
- Using dynamic Object Properties
- Accessing the parent Context in Nested Loops
- Defining undefined Functions, Filters, and Tags on the Fly
- Validating the Template Syntax
- Refreshing modified Templates when OPcache is enabled
- Reusing a stateful Node Visitor
- Using a Database to store Templates
- Using different Template Sources
- Loading a Template from a String
- Using Twig and AngularJS in the same Templates