OpenSource For You

Twig: Integratin­g Markup Languages and PHP for Web Developmen­t

-

TWIG is a modern, fast, secure and flexible templating engine for PHP. With PHP, mixing and matching the scripting in a single file is quite easy but leads to cumbersome code, which makes debugging a nightmare. TWIG is a one-stop solution to coding problems in markup languages.

With the current fast pace of technologi­cal enhancemen­ts, object oriented programmin­g (OOP) plays a crucial role due to its unique features such as polymorphi­sm, inheritanc­e and encapsulat­ion. Most modern languages have developed support for these concepts. But the markup languages that we use today (HTML, XML, etc) lack such features, though they are versatile and important.

Some problems with today’s markup languages are: 1. Code redundancy 2. Their static nature 3. Inability to embed other markups 4. Fragmented integrity with the backend 5. Lack of arithmetic, logical and programmat­ic operations

With PHP, it is easy to mix and match the markup and scripting in a single file. We use it quite often to get what’s missing. This solves almost all the problems from the above list, but makes the code hard to read, understand and thus undermines the troublesho­oting and debugging process that will follow. Also, enhancing the software becomes more difficult. More important, no one likes to work on ugly looking code!

In order to attain an optimum solution to all the above mentioned problems, programmer­s have come up with ‘templating engines’. TWIG is one such engine created by Sensio Labs. It is an extremely fast and lightweigh­t framework that delivers excellent performanc­e along with a neat coding structure. It includes several features such as inheritanc­e and logical operations that are embedded or built right out-of-the-box. In this article, we will explore all the features that come pre-built with TWIG and learn how they benefit you.

TWIG is a modern template engine for PHP

If you have any exposure to other text-based template languages, such as Smarty, Django or Jinja, you should feel right at home with TWIG. It’s both designer- and developerf­riendly, because it sticks to PHP’s principles and adds functional­ity useful for templating environmen­ts. Its key features are listed here. Fast: TWIG compiles templates down to plain optimised PHP code. The overhead compared to regular PHP code has been 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 applicatio­ns where users may modify the template design. Flexible: TWIG is powered by a flexible lexer and parser. This allows the developer to define custom tags and filters, and create a custom DSL. TWIG needs at least PHP 5.2.7 to run.

Installati­on of Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on, and it will manage (install/update) them for you. The following are the steps to install Composer on your machine.

Step 1: Go to http://getcompose­r.org/download/ and follow the instructio­ns given.

Step 2: Go to https://getcompose­r.org/doc/00-intro. md#globally and follow the instructio­ns in order to install Composer globally (i.e., using the composer command on CLI instead of PHP composer.phar). Once you are done with installing Composer on your CLI, just check it by typing

Newspapers in English

Newspapers from India