Twig: Integrating Markup Languages and PHP for Web Development
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 technological enhancements, object oriented programming (OOP) plays a crucial role due to its unique features such as polymorphism, inheritance and encapsulation. 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 programmatic 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 troubleshooting 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, programmers have come up with ‘templating engines’. TWIG is one such engine created by Sensio Labs. It is an extremely fast and lightweight framework that delivers excellent performance along with a neat coding structure. It includes several features such as inheritance 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 developerfriendly, because it sticks to PHP’s principles and adds functionality useful for templating environments. 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 applications 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.
Installation 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://getcomposer.org/download/ and follow the instructions given.
Step 2: Go to https://getcomposer.org/doc/00-intro. md#globally and follow the instructions 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