David Corbacho

Drupal with Twig templates

It's not a secret that Drupal 8 will most likely have in core the option of using the Twig templating system.

Discussion concerning Twig inclusion happened quite fast between March-April 2012. See this issue: [meta] New theme system, where Dries commented: "I just wanted to let everyone know that I'm in support of rethinking the theme system. I'm also comfortable that the team will come up with a good solution."

Why use a template system when PHP can be already be used as a template system?

The reason to use a template system is mainly to separate the presentation from business logic. When you use PHP as template system you are giving too much power to the presentation layer. So, it's easy for things to get messy mixing code and layout, and even worse: having security issues in custom themes.

John Albin stood up in Denver and said "We hand themers a loaded gun and tell them to hammer in a nail with it. Oh, and be careful"

A template engine enforces a clean separation, using a tag-based syntax, so no PHP knowledge is needed and improves reusability of templates.
Web designers with non-programming background are the ones who most will appreciate this separation. Also this will prevent them from shooting themselves in the foot, compromising the security of your web site.

If you still are not sold, take a moment to read these two articles and comments discussion of the convenience of a template engine that is not PHP:

Templating Engines in PHP, and the follow up. By Fabien, main developer of Symphony and Twig.

Twig

Twig is quite powerful, supporting macros, i18n, extensions, template inheritance, etc. From a developer standpoint, Twig is that complex that almost in the line of "what's the point of learning such a complex templating language when we have PHP?"

But from a designer point of view it's more about "I have complete control to do literally anything with the content without having to bother the programmer at all".

Pros

  • The main benefit of bringing Twig to Drupal will be security. The rest of pros/cons are not as important as providing a default secure HTML output.
  • Powerful, expressive template language with easy syntax and very cool features
  • It's widely supported among IDEs (Eclipse, PHPStorm, etc).
  • Not NIH syndrome in Drupal community: It will bring Symphony and Drupal communities closer, since we are sharing more and more components
  • Re-use in JavaScript? There is a github repo at least that provides Twig in JavaScript .

Possible Cons

  • A little steeper learning curve for Drupal.
  • (Just wondering) How tightly coupled is Twig with Symphony?
  • Performance and memory could be an issue (See http://www.phpcomparison.net ), although the bottenecks of your website probably are in other places.
  • (Just wondering) Headaches about versioning? If Twig is evolving constantly, how Drupal 8 core will handle it when development gets frozen.

Drupal and Twig

Some implementation details are in this post Drupal 8 Theme System Sprint. (Jacine, chx, and John Albin)

* chx's sandbox: Drupal 8 with twig

For Drupal 7 we also have these two experimental modules:

* ReneB's sandbox Twig for Drupal (Twig implemented as a theme engine)
* smokris' Twig template engine (Twig implemented as an input filter)

See also

ReneB presentation slides: Twig for Drupal @ Frontendunited Amsterdam 2012

Note: When googling Twig, notice that most of articles, benchmarks, etc are based on old versions of Twig.
Twig has evolved a lot in the last months.