Clarity and speed in template development with Timber

Accelerating WordPress theme development with Timber

WordPress theme development is largely an open sandbox. The core WordPress code provides template tags and “the loop” but outside of these general structures developers are left to their own devices. Many choose to write their theme templates in the standard HTML and PHP fashion. But that’s not the only way to go about writing themes.

Timber http://upstatement.com/timber/ is a WordPress plugin that allows templates to use both the Twig template engine and some helpers to merge WordPress functions into Twig. We’ve been using it for a few projects and have found real benefits to bringing in an outside templating engine this way.

Clearer Separation of Concerns

Twig templates cannot contain PHP; it merely renders HTML from a “context” sent to the template. This forces us effectively adopt a MVC style of separation of concerns. Everything going to be displayed on a page has to be packaged into the context before it can be sent to the Twig template via the page’s WordPress PHP template.

It’s not as strict as true MVC would be, but it does help to ensure we make good code design decisions. It’s much easier to write good, clear code when the system you are writing it in encourages that good practice.

Easier Teamwork

We often have dedicated front-end developers working alongside us on our projects. PHP knowledge is not required of them and nor should it be. Twig is a much more approachable and obvious way of working with templates than a mess of PHP.

{{ post.title }} is much easier to work with than <?php the_title(); ?>.

This lets us bring in our front-end crew earlier and allows them to be more effective and independent. The PHP developers focus on building the Twig contexts and passing the correct data to the Twig templates. The front-end developers in turn focus on getting their end right.