danmoorhouse.uk web development services

Timber and Twig for WordPress: why I like separating logic from templates

By Dan Moorhouse on 08/06/2026 in Development - Web Topics


WordPress theme development can become messy quickly.

A template starts with a simple loop. Then a few custom fields are added. Then some conditional checks. Then a query. Then some formatting logic. Before long, the template is doing too much. It is retrieving data, preparing data, checking rules, and rendering HTML all in the same file.

That works for small builds, but it does not scale well.

This is one of the reasons I like using Timber and Twig in WordPress. It gives WordPress theme development a cleaner structure. PHP can handle the logic. Twig can handle the template. Each part has a clearer job.

My background with Twig

Before working with Twig in WordPress, I used it for around four years as a Drupal developer.

Drupal uses Twig as its template engine, so it became a normal part of my day-to-day development work. Page templates, node templates, paragraph templates, fields, menus, regions and layout components all relied on Twig in some form.

That experience shaped how I think about theme development.

When I later worked in a WordPress agency environment, Timber felt like a natural choice. It brought a familiar structure to WordPress theme builds. Instead of mixing large amounts of PHP into theme files, I could work with a clearer separation between data preparation and presentation.

That made the code easier to read, easier to maintain and easier to hand over.

What Timber does

Timber allows WordPress themes to use Twig templates.

In a traditional WordPress theme, a PHP template might fetch content, prepare variables, run conditionals and output HTML. With Timber, the PHP file prepares the context and passes it to a Twig file.

The PHP file focuses on what data the template needs.

The Twig file focuses on how that data is displayed.

A cleaner template layer

Twig templates are easier to read because they are not full of PHP logic.

A template can focus on markup, layout and small presentation checks. That makes it much easier to scan a file and understand what is being output.

For example, a service card template does not need to know how a service was queried. It only needs to know that it has a title, image, excerpt and link.

That keeps components focused.

It also makes templates easier to reuse. A card can be used on a service archive, a homepage section, or a related content block, as long as the data passed to it follows the same structure.

Better separation of concerns

Separation of concerns is one of the biggest benefits of using Timber and Twig.

Theme development usually involves three broad layers.

  • Data retrieval
  • Data preparation
  • Presentation

When these are mixed together, templates become harder to maintain. A small design change can involve reading through unrelated queries and logic. A content change can affect markup. A new feature can create duplicate code across multiple templates.

Timber helps keep those responsibilities apart.

The PHP layer can decide what data is needed. It can fetch posts, read custom fields, prepare arrays, apply fallbacks and organise values.

The Twig layer can then render the result.

This makes the codebase easier to reason about. Each file has a clearer purpose.

More structured WordPress development

WordPress is flexible, but that flexibility can lead to inconsistent theme code.

Timber helps bring structure to that flexibility.

It encourages a more deliberate approach to templates, components and reusable patterns. Instead of building every file as a one-off PHP template, a theme can be broken down into smaller Twig files.

A header can include navigation templates.

A service page can include content sections.

A card grid can include a reusable card component.

A project page can include shared media blocks.

This works especially well with custom fields. ACF and Timber together provide a clean way to build flexible WordPress themes without turning templates into long PHP files.

Easier maintenance

Websites change. Services are renamed. Layouts are updated. New sections are added. Content models evolve. Design systems grow.

A well-structured Timber theme makes those changes easier.

When logic lives in PHP and markup lives in Twig, it becomes quicker to find the right place to make a change. Developers do not have to untangle everything from one large template file.

It also reduces duplication.

Shared Twig components can be updated in one place. If a card layout changes, the update can apply across every part of the site that uses that component.

That saves time and reduces the risk of inconsistent layouts.

Cleaner handover between developers

A structured Timber theme is easier for another developer to understand.

That matters in agency work, freelance projects and long-term support. A website is rarely touched by only one person forever.

When a theme has a clear structure, another developer can open it and understand the pattern. PHP prepares the data. Twig renders the view. Components live in predictable places.

That is easier to work with than a theme where every template has its own mixture of queries, fields, HTML and display logic.

It also helps when returning to a project months later. Clear structure reduces the time spent relearning how the theme works.

Not about making WordPress something else

Using Timber does not mean trying to turn WordPress into Drupal or another framework.

Posts, pages, custom post types, taxonomies, hooks, filters and plugins all remain part of the build. Timber simply gives the theme layer a cleaner way to work.

For developers who have worked with Twig in Drupal, Symfony, Craft CMS or other systems, it also makes WordPress feel more familiar. The template layer becomes more consistent with modern PHP development patterns.

Why I prefer it

Timber and Twig is my go-to setup because they make WordPress theme development feel more organised.

  • Reduces clutter in templates
  • Components are easier to reuse
  • Logic and presentation separate
  • Make themes easier to maintain after launch

Timber gives WordPress a cleaner theme development workflow while still keeping the strengths of WordPress intact.

Check out the Timber V2 docs at: timber.github.io/docs/v2/

Twig docs: twig.symfony.com/doc/3.x/


Share this post