Twig - the Most Popular Stand-Alone PHP Template Engine

Twig - the Most Popular Stand-Alone PHP Template Engine

This article was peer reviewed by Wern Ancheta. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

Twig is a template engine for PHP. But isn’t PHP itself a template engine?

[…]

PHP is a verbose language, and that verbosity is amplified when trying to output HTML content.

[…]

To exemplify this, let’s separate actual content from the HTML definition in our example. Let’s create a new HTML file and call it layout.html:

[…]

Let’s cache the templates in our example by editing our bootstrap.php file:

[…]

This operator is just syntactic sugar for the range function which works just like the native PHP range function.

[…]

The js, CSS, URL and html_attr escaping strategies are also available. They escape the string for the Javascript, CSS, URI and HTML attribute contexts respectively.

[…]