Getting started with Twig template engine usage

Source: Internet
Author: User
Tags autoloader php template
This article mainly introduces the use of the Twig template engine and analyzes the basic functions, installation, and usage of the Twig template engine. For more information, see the following example. We will share this with you for your reference. The details are as follows:

Introduction

Twig is a flexible, efficient, and secure PHP template engine.

If you have used text-based templates such as Smarty, Django, or Jinja, Twig is a natural thing. Twig strictly abides by PHP's belief and adds useful functions in the template environment. these practices make Twig very friendly to designers and developers.

Twig has the following main features:

Efficiency: Twig compiles the template into an optimized PHP file. compared with the native PHP code, the performance consumption is very small.

Security: Twig uses sandbox mode to run untrusted code in the template. This allows us to select Twig as the template engine for applications that allow users to modify templates.

Flexible: Twig has a flexible syntax analyzer and syntax parser that allows developers to define their own tags and filters and create their own domain-specific languages (DSL, domain specific language ).

Prerequisites

The minimum PHP version required by Twig is 5.2.4.

Install

There are multiple ways to install Twig. If you are not sure which one to use, download the compressed package directly.

Package Installation

Download the latest compressed package from the download page

Extract

Place the extracted files to a place that can be accessed by the project.

Install the development version

Install Subversion or Git

SVN address: http://svn.twig-project.org/trunk/, git address git: // github.com/fabpot/Twig.git

Install using the PEAR package

Install PEAR

Pearchannel-discoverpear.twig-project.org
Pearinstalltwig/Twig (or pearinstalltwig/Twig-beta)

Basic API usage

This section briefly introduces a Twig php api.

The first step to use Twig is to register its autoloader:

require_once '/path/to/lib/Twig/Autoloader.php';Twig_Autoloader::register();

Remember to replace/path/to/lib with the path where Twig is located.

Note: Twig complies with the PEAR conventions in class naming, which means that you can integrate the loading of Twig classes in self-compiled autoloader.

$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, and uses 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). the object can be rendered using the display () method.

Twig can also use the 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');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.