Because WordPress is awesome,
but the loop isn't.

What does it look like?

With Timber you manage your theme in PHP and Twig (HTML) files. This separates the logic (getting stuff from WordPress) from presentation (adding tags, classes, etc.) — just like Rails, Django, Node and other platforms.

							
							$context = Timber::get_context();
							$context['foo'] = 'Bar!';
							$context['post'] = new TimberPost();
							Timber::render('single.twig', $context);
							
						
single.php
							
								{% extends "base.twig" %}
								{% block content %}
								<h1 class="big-title">{{foo}}</h1>
								<h2>{{post.title}}</h2>
<img src="{{post.thumbnail.src}}" />
<div class="body">
     {{post.content}}
</div> {% endblock %}
single.twig

SensioLab's Twig Template Engine gives you a simple interface to WordPress's intense power.

What does it look like? (Part 2)

Timber doesn’t change the appearance of your theme. You control it by creating classes and assigning styles. It’s like buying a chainsaw — it doesn’t make your trees look different ‘till you start using it.

  • Good for front-end devs/designers

    Twig's beautiful syntax makes it easy to build your HTML and CSS without limit. It's easy to swap in new HTML for different sites or themes. That's less copy-and-pasting-and-rewriting and more extensibility.

  • Good for teams & sharing

    Timber is built with teams of designers and developers in-mind. Not everyone knows the ins and outs of the_loop(), WordPress, and PHP. With Timber, your best WordPress dev can focus on the PHP, and your designers can work in much cleaner HTML markup.

  • Good for WordPress!

    WP sometimes gets a bad wrap because it's so easy to get started and make something (this is a good thing!). What if we could keep this shallow learning curve, but give other users a more solid platform to build on? Your new super-clean WordPress code will help you (and your clients) build-on the site for years to come.

Technical highlights

Object Oriented Posts, Terms, Users and More

Speak to the common WordPress objects in a way that makes sense. Have you ever tried to get the thumbnail of a post? Don't lie to me, of course you have! It probably looked something like this:

The old way

							$thumb_id = get_post_thumbnail_id($post->ID);
							$url = wp_get_attachment_url($thumb_id);
							<img src="<?php echo $url; ?>" alt="Thumbnail for <?php echo $post->post_title; ?>" />
						

Yuk! Maybe you can remember all those functions, but I sure can't. If you're a pro WordPress dev you probably have a mess of shortcut functions to help you with a common task. But here's the rub YOUR shortcuts are going different than everyone else's.

but with Timber

							{# single.twig #}
							<img src="{{post.thumbnail.src}}" alt="Thumbnail for {{post.title}}" />
						

You can find the full object reference here.

Express/Node-style routing

When I first used routes in Node.js I fell in love. But when I tried to do the same, WordPress broke my heart.

The old way

							add_rewrite_rule('jobs/page/([0-9]+)?$', 'index.php?pagename=jobs&paged=$matches[1]', 'top' ); 
						

I'm sure if you're a regex pro, this makes total sense. But most of us aren't regex pros. There's got to be a better way!

with Timber

							Timber::add_route('jobs/list', function($params){
									$query = 'posts_per_page=5&post_type=jobs';
									query_posts($query);
									Timber::load_template('archive.php');
							});

							Timber::add_route('jobs/list/page/:pg', function($params){
									$query = 'posts_per_page=5&post_type=jobs&paged='.$params['pg'];
									query_posts($query);
									Timber::load_template('archive.php');
							});
						

A few extra lines? Guilty! But clear as hell? You betcha. Read more about Custom Routes

It's a library, not a Framework

The tools I've come to rely on are the tools that evolve into your workflow. I didn't know I wanted to use jQuery or SASS until I started using them. Bit-by-bit they replaced the 'old' ways of doing things until I couldn't imagine life without them. Timber lets you start with just a bit. Say you want to just include an HTML snippet with a variable from your database:

home.php

							$data['welcome_message'] = get_option('welcome_message');
							Timber::render('welcome.twig', $data);
						
Inside your twig file

							{# welcome.twig #}
							<p class="intro">{{welcome_message}}</p>
						

Check out the Screencasts for a walk through of how to start integrating Timber, today!

Timber is a free open-source project for everyone who uses WordPress. The best payment is to Tweet and star the project on GitHub so others can find it and contribute.