Making Wordpress great again with Timber

Digiti
By Digiti
Published in
4 min readNov 26, 2018

Wordpress has been the most popular CMS for a while now. At Digiti, we often propose Wordpress to our customers because of its ease of use. But core Wordpress has been leaping behind on some fronts.

One of the biggest caveats I had with Wordpress is how it almost forces you to mix logic and theming. In most themes, you find a query for posts right in the middle of your HTML code. Who likes that kind of thing in the age of templating frameworks and “separation of concerns”?

Introducing Timber…

For the past year, we have been using Timber for our Wordpress setups. Timber is a Wordpress plugin that brings the widely adopted Twig templating engine to Wordpress, together with a lot of other nifty things that will bring your development to a whole new level by helping you write more sustainable code, faster.

Timber is both available as a Wordpress plugin and a composer package you can load into your theme. There are some starter themes available to help you get started as well.

For more information take a look at the documentation: https://timber.github.io/docs/getting-started/setup/

Now let’s take a look at the things I like the most about Timber…

Bringing an MVC-like structure in your theme

Timber provides an MVC-like structure to your theme. It’s not really MVC because your “controller” only contains the code that 1 method from a real controller would contain.

Model: Timber provides a default class “TimberPost”, which is a beefed up version of the WP_post class that you can extend yourself.

View: your twig file with all of your HTML.

Controller: the default .php template files (index.php, home.php, single-post.php, …) will act as your controller, you can retrieve your posts and call the correct twig template in it.

Base Templates and Partials

With the power of Twig in your Wordpress theme, it’s really easy to separate a base template and extend it in our page templates.

In your base template files, you can put all the stuff that’s available on each page and define some regions. After that you can extend this template in your page template, filling in all the specifics for the defined regions.

Routing

Need some custom routing? No problem, Timber can help you define your own routes.

Easy thumbnails

Wordpress way of dealing with thumbnails can be a bit frustrating when you add new sizes when there’s already a bunch of images uploaded. With Timber, you can just generate the thumbnail you need on the fly.

No more nav walkers!

Finally no more headaches of trying to control the output of your menus. With Timber, you can render your navigation menus in a sensible way.

All the Twig goodness

Timber brings all the twig goodness like filters and loop variables to you.

Conclusion

The above is only a gist of what Timber offers, there are much more nice things available like easy caching and Advance Custom Fields (ACF) compatibility.

If you are doing any custom Wordpress development and find yourself struggling with Wordpress old paradigms you should really give Timber a try, you won’t regret it!

Do you wish for help with your Wordpress website? Let’s talk! We’d be happy to help.

--

--

Digiti
By Digiti

We are Digiti, the kind of people who love, breathe and live for all things digital.