Symfony2 and Twig work great together. Symfony is an advanced PHP framework that is growing in popularity. If you are a company that relies on rapid application development and need a stable and secure development and production environment, Symfony is one of the top choices to consider. More on Symfony another time, we’re talking Twig today.
What is Twig and why should I learn it?
Twig is straight forward and has a pretty quick learning curve. Twig is a templating system that is intuitive and really does help reduce the amount of code while keeping your views neat and more importantly, functionally separate from your business logic, which is always what good “V”s — the “V” in “mVc” — is supposed to do. Twig isn’t a language, it’s a set of rules and constructs based on simple programming patterns that introduce only 3 new tags: {% do something %}, {{ show something }}, {# comment on something #}. These tags help you create editable blocks based on a base template. If you use Dreamweaver templates or some other template system, adapting to Twig and the concept of extending and overriding base templates will be nothing new. It’s powerful because it’s simple to follow the cascading logic that so much of the web works from.
Basics behind Twig templating
The concept behind templates and cascading style sheets are all the same. For simplification, let’s say you have a base template that can have anything in it, including the base HTML code for a basic page in your website. We’ll represent that as a simple-to-follow pattern where each number represents a different important part of the webpage, like the header, metatags in the header, top menu, sidebar, footer, etc. However you want to logically group your pages into manageable blocks, so let’s say your template is based on a simple construct: “1 – 2 – 3 – 4 – 5 – 6 – 7 – 8 – 9”. When you create a new template based on this base template, you only tell the system what’s different and show the rest as is. So in simplified terms if your new template has a command that says “5=A”, the new output from a template will look like this: “1 – 2 – 3 – 4 – A – 6 – 7 – 8 – 9”. Basically, instead of writing out all the code for each page over-and-over, you practice the DRY (Do Not Repeat Yourself) principle and use a template to override the parent, or base template.
In your new child template, you can use powerful commands to change a lot from the base with little code. In your child template, you can say things like “change all dashes to tildes” — and one command can change your template to the new pattern “1 ~ 2 ~ 3 ~ 4 ~ A ~ 6 ~ 7 ~ 8 ~ 9”. You get the point, templates are all about making the output of your views easier to reuse and maintain. Not to mention they’re not nearly as scary to non-coders who have to poke around and update little things in your website, you know they can’t alter global variable values or do other things that could alter the business logic of your web application(s).
Twig provides rules and structure that consolidate mainly view functions based on the most common PHP capabilities, like iterators — a way to “loop” through values and show a list on screen quickly and easily. A way to sort or filter your output without sending a new query and waiting for a response from your database. It can reduce requests and server loads by being more efficient with your website’s resources. Twig also works best with PHP Storm IDE since it has great live validation and helpers. PHP Storm is a potent combination for serious PHP web developers using Symfony, Twig, and LESS as well.
Your post demonstrates a deep understanding of the subject matter. It’s evident in the comprehensive coverage and detailed explanations. To learn more, click here.