ClassicPress PetitionsClassicPress Petitions
This is a read-only archive. Post or comment on the live version of this page on our forums.

Add a templating engine like Twig

December 1, 2018 · 15:16 · rkoller

In the current state templating is plain spaghetti code. PHP interwoven with HTML. Readability is difficult at best. With a solution like Twig things would get dramatically improved. For WordPress already exists a plugin: . With its help you get a separation of concerns. You have PHP files responsible for the logic and control and twig files for the presentation. Way easier to read any maintain, improved security through Twig by default, way easier loop construction and in case you ever need to work with time and unix timestamps in WordPress the Twig functionality is nothing but bliss. And the best it is backward compatible. Themes could run built in the old fashioned way without using Twig functionality at all, you could update certain templates adapting to Twig or migrate or write themes from the ground up for Twig.

+5 more
Difficulty: Hard
Request: Add feature
Pieter Bos

Apart from the fact that I'm not a fan of Twig, I also don't think that adding this into the equation would be a good thing. People have yet to learn something new, basically for no reason at all. There are other proposals that tackle decoupling and are far superior in both method and outcome.

James Nylen

@Pieter: how would you recommend decoupling HTML structure from dynamic content?

I like template engines personally. For many projects in JavaScript, I've used a very similar library:

If you would like to use Twig in a WordPress/ClassicPress theme, by all means, feel free. Much like SCSS/SASS, you'll need to make it part of your development workflow, and distribute the final, compiled PHP files produced by Twig as part of the theme package.

However, there are a couple of things that make the inclusion of a template engine not so great for ClassicPress core.

First, the core code does not even try to separate concerns - it's all just PHP, filters, HTML, CSS, and some inline JavaScript mixed together however someone thought it was a good idea at the time. This means introducing a template engine to the core software would be a major re-organization of basically any code structure that outputs HTML.

For ClassicPress itself, we don't have the resources to complete this task right now. Also, we are trying to keep ClassicPress compatible with WordPress, but rearranging the order of filters and HTML output would break a decent number of plugins.

Second, using Twig creates a bunch of generated PHP code. I understand why, but this is more code that needs to be reviewed and kept up to date. In the best-case scenario this adds a new build step, which is probably going to be required to develop against ClassicPress.

I am very interested in keeping ClassicPress easy to develop against. Ideally this means that build steps should only add "optional" value like minifying CSS and JS, but no build steps should be necessary to run and modify the core software.

Paolo Falomo
Xander Bertison

I'd also suggest Blade over Twig. It's actually already been done by the team over at Roots.

Their starter theme allows for anyone to use Blade instead of regular PHP.

@James Nylen this creates a bunch of generated PHP code also, but it does not do so as a build step. It does it like how Laravel does it and just generates it and stores it in a cache directory as the page is first loaded.

However, seeing as this is completely possible to do just by installing a theme, I'm not sure it's anything even needed in ClassicPress core.

It might be a cool idea to maybe work with the folks over at Roots on some sort of good tight integration. WP includes some awful default themes, CP could include Sage as an awesome, but optional, default theme.

Could also be cool to get them to offer a Bedrock version of CP.

James Nylen

Generated PHP code makes me a bit nervous because it is an attack vector that is very difficult to detect and prevent.

Still, I think this is a good candidate for an experimental theme under This way it will at least be available as an option for people who want it.

James Nylen

Rather than force a specific templating engine upon users, why not implement a real templating system that just comes with a PHP engine by default? Let plugins provide other engines and let the user decide what they want to use.

Guy Phillips

I agree no Twig. Why introduce a complete new structure. I inherited a twig site and I hate working with it.

Peter Molnar

Twig is awesome. I made a tiny theme with twig a long while ago ( ), it was way faster, than using the normal WP functions.

Also, Twig is near drop-in compatible with some other language templating systems, eg. jinja2, which could benefit porting projects.