Introduction - Documentation - Twig - The flexible, fast, and secure PHP template engine


本站和网页 https://twig.symfony.com/doc/2.x/intro.html#installation 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Introduction - Documentation - Twig - The flexible, fast, and secure PHP template engine
About
Docs
Dev
Twig
The flexible, fast, and securetemplate engine for PHP
Docs
Introduction
You are reading the documentation for Twig 2.x.
Switch to the documentation for Twig
1.x.
3.x.
Table of Contents
Prerequisites
Installation
Basic API Usage
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
Twig documentation is licensed under the
new BSD 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.
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.
Screencast
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 {{ 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', ['name' => 'Fabien']);
« Twig
Installation »
Website powered by Symfony and Twig, deployed on
The Twig logo is 2010-2022 Symfony