Twig template engine usage Getting Started tutorial _php instance

Source: Internet
Author: User
Tags autoloader php template
The example in this article describes the use of the Twig template engine. Share to everyone for your reference, 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 think Twig is a natural thing to do. Twig strictly adheres to PHP's beliefs and adds useful functions in the template environment, which makes twig very friendly to designers and developers alike.

The main features of Twig are:

efficient : Twig compiles the template into an optimized PHP file, which, compared to the native PHP code, has a very low performance loss.

Security : Twig use sandbox mode to run code that is not trusted in the template. This allows us to choose twig as the template engine for applications that allow users to modify 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, download the zip package directly.

Compress Package Installation

Download the latest Zip package from the download page

Extract

Place the extracted files where the project can be accessed.

Install the development version

Install subversion or git

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

Installing with the Pear package

Installing Pear

pearchannel-discoverpear.twig-project.org
Pearinstalltwig/twig (or Pearinstalltwig/twig-beta)

Base API Usage

This section will give a brief introduction of 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's path.

Note: Twig adheres to the pear conventions in the naming of 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 ');

Read more about PHP template related content Readers can view this site: "PHP Template Technology Summary"

I hope this article is helpful to you in PHP programming.

  • Related Article

    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.