an-introduction-to-twig-templating

Basics of Twig Templating for friendly development

Twig the modern template engine for PHP

We know that PHP also itself is a template engine but it doesn’t support many features that modern template engines have nowadays. So, there was need to design something special for PHP templates.I am going to discuss some basics of Twig Templating .

Twig is a fast, secure, optimized, flexible, fully documented and easy to learn template engine for PHP. The most necessity was to design it to make creating templates easier on both the developer and the designer. Twig allows the developer to define his own custom filters and tags.

Basics of Twig Templating

Twig is Easy:

Its syntax is easy. The processes are straightforward which make it a natural fit for anyone who have exposure to other text-based template languages, such as Smarty, Django, Jinja, Liquid, or Stencil.

Twig is Secure:

Twig facilities the developer with a sandbox mode that allows them to observe any untrusted code. So, it gives the ability to the developers to use Twig as a template language for applications while giving users the ability to modify the template design.

Literally, it is a most powerful engine that gives us control over the user interface.

Twig is Concise:

The PHP language is wordy and becomes ridiculously wordy when it comes to output escaping.

In comparison, Twig has a very concise syntax, which make templates more readable:

Twig has template oriented syntax:

It has shortcuts for common patterns, like having a default text displayed when you iterate over an empty array:

Twig is full featured: Twig supports everything you need to build powerful templates with ease: multiple inheritance, blocks, automatic output-escaping, and much more:

Prerequisites

It needs at least PHP 5.2.7 to run.

Installation

The recommended and best way to install Twig is via Composer:

Basic API Use
Basic Example of Twig Template: