Twig + WordPress = Timber

• ~300 words • 1 minute read

One of the things that bothers me about making WordPress themes is that there isn't always a clear delineation between logic and presentation. Some of that is due to the pseudo-template nature of PHP itself and some of that is because WordPress is just a weird, fickle beast that's the result of nearly ten years of open-source development with an admirable (if sometimes frustrating) dedication to preserving compatibility between versions and with older server environments.

Basically though, I want the templating aspects of WordPress to be more similar to the templating experience I get with Shopify. I've written a little about this in a post about using Swig for templates in Node but for whatever reason never thought to look for a similar solution in WordPress.

Instead of adding it to my long-list of open-source weekend projects to start I did some searching and stumbled across Timber. It's a WordPress plugin that uses the Twig template system which is very similar to Swig, Liquid and probably a dozen other template systems out there. The learning curve isn't particularly steep and for basic themes I suspect it will rapidly speed-up production.

By separating the logic and presentation into separate files — logic essentially stays in the files you've come to know in the WordPress template hierarchy while the presentation gets moved into .twig files into a folder called views, essentially fulfilling the vc portion of the mvc paradigm — it allows me to more cleanly manage themes and projects that could have custom, web-app-like functionality (There is a school of thought that would suggest moving a lot of this stuff into a custom plugin instead of the theme. In principle I agree but in practice... I just don't. In my experience it just further fragments the pieces of the project in an unhelpful way.).