The examples in this article describe the Twig template engine usage. Share to everyone for your reference, specific as follows:
Introduced
Twig is a flexible, efficient, and secure PHP template engine.
If you've ever used a text-based template engine such as Smarty, Django, or Jinja, you'll find twig a natural thing. Twig strictly adheres to PHP's beliefs and adds functions that are useful in the template environment, which makes twig very friendly to both designers and developers.
The main characteristics of Twig are:
High efficiency : Twig the template into an optimized PHP file, compared with native PHP code, the performance loss is very small.
Security : Twig uses sandbox (sandbox) mode to run untrusted code in the template. This allows us to choose twig as the template engine that allows users to modify the application of the template.
Flexible : Twig has a flexible parser and parser that allows developers to define their own tags and filters (filters) and create their own domain-specific language (Dsl,domain specific language).
Necessary
The minimum PHP version required for Twig is 5.2.4.
Installation
There are several ways to install twig. If you are not sure which one to use, then download the compressed package directly.
Compressed Package Installation
Download the latest zip pack from the download page
Extract
Place the extracted files where the project can be accessed.
Install development version
Install subversion or git
SVN address: http://svn.twig-project.org/trunk/, git address git://github.com/fabpot/twig.git
Installing with Pear Package
Install Pear
pearchannel-discoverpear.twig-project.org
Pearinstalltwig/twig (or Pearinstalltwig/twig-beta)
Basic API Usage
This section gives a brief introduction to a twig PHP API
The first step in using Twig is to register its autoloader:
Require_once '/path/to/lib/twig/autoloader.php ';
Twig_autoloader::register ();
Remember to replace/path/to/lib with Twig path
Note: Twig adheres to Pear's conventions in naming classes, which means that you can consolidate the loading of twig classes in the autoloader you write .
$loader = new twig_loader_string ();
$twig = new Twig_environment ($loader);
$template = $twig->loadtemplate (' Hello {{name}}! ');
$template->display (Array (' name ' => ' Fabien '));
Twig uses the loader (twig_loader_string) to locate the template while using the environment (twig_environment) to store configuration information.
The LoadTemplate () method uses the information set by the loader to locate and load the template, and returns a template object (Twig_template) that can be rendered using the display () method.
Twig can also use the File system loader (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 ');
More about PHP templates interested readers can view the site topics: "PHP Template Technology Summary"
I hope this article will help you with the PHP program design.