Timber: Utilising Twig in Wordpress

I’ve mentioned before that I’ve been playing with Symfony; one of the cool things about that  framework is how the Twig templating language is fairly deeply integrated into it.

For those who don’t know, Twig is a way of writing templates that is designed to separate function from form in an easy to understand way. Controllers pass information into the templates which use a little bit of logic and variable placeholders to display that information to the user. The big draw is that all this logic is written in fairly plain english using a curly braces – a bit like “Mustache”.

So, for example, instead of writing:

$var = "FOO";
echo $var;

You’d set $var in your controller and write your twig template as:

{{ var }}

It’s really cool and, like I say, really well integrated into Symfony. You can read a bit more about it here.

But I’m also a WordPress guy – I’ve used the blogging platform for many years now to build sites and I wondered if it was possible to use Twig to make the templating in WordPress a bit… nicer.

See, WordPress doesn’t really do MVC that well. MVC is the coding principle of Model, View, Controller, whereby all data objects, output and “doing” code are kept in their own separate place making things easier to manage. Symfony is an MVC framework and it makes building reusable code really easy. WordPress kind of handles a lot of model and controller elements within its core and it’s fairly easy to extend this with plugins and themes, but I always find it’s hard to get away from actually including “doing” code within those themes which can get a little… messy.

So, Twig. Well, it turns out there is a fairly respected plugin for WordPress called Timber (website is here) which enables the use of Twig within WordPress templates. I rolled a quick test site to have a go and, I have to say, I’ve come away in two minds.

Firstly, don’t get me wrong – the idea of improving the readability of WordPress themes is certainly massively appealling. I’m just not sure the execution of Timber is really the answer. In the past I’ve built sites using stacks of plugins, mainly as a matter of necessity, having to deliver on projects, but over the last few years this is something I’ve been trying to steer clear from. Building something where an element as essential as the display layer hinges on the performance of a single plugin now seems quite detrimental to me – a single point of failure that could bring down everything is a bit of a scary prospect. The actual implementation also seems a little… fiddly. In principle you can utilise Timber alongside traditional themes, but the process of integration introduces a lot of new layers to consider and changes to the makeup of your theme. Yeah, I know – that’s the point! However, having done some deep dives into implementing fiddly functionality into WordPress themes in the past I’m wondering how long you’ll hit a point where using Twig becomes a hinderance rather than a benefit.

Anyway, Timber is a fairly popular plugin with a lot of high profile users and WordPress templating sure as hell needs a kick into being a bit more user friendly, so I think it’s likely worth more invetigation, however it’s a shame that I’ve initially come away with a feeling of disappointment rather than elation.

Do you use Timber? If you’ve got any thoughts on it, drop some comments below!

Advertisements

2 comments

  1. It will be very interesting to see how twig templates are implemented in WordPress as normal WordPress templates make heavy use of global variables and loops. But it is a good idea to have twig as a template engine as an alternate option.

    • I think this is what’s kind of made me a little hesitant – there’s some great potential here but there’s also a few WordPress foibles that could make the overall implementation… fiddly…

      I do like Twig, though…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s