WordPress Templates Are Broken!

(But We Can Fix Them)

Jared Novack

@jaredNova

From STL now in Boston

WordPress dev since 2010

I love WordPress

Things to love about WordPress

Oh, love that Admin

Things to love about WordPress

The best community

Things to love about WordPress

Clear and sensible template hierarchy

  • index.php
  • home.php
  • page.php
  • page-about.php
  • single.php
  • single-portfolio.php
  • archive-portfolio.php

Let's start Theming!

TwentyTen Theme

TwentyTwelve Theme

We have a word for this...

I'm a WordPress Developer

Ruby on Rails

Django

Laravel/Symfony/Zend

Node.js

Yes, even Drupal

A Template Language

<div class="entry">
<h1>Hello World!</h1>
<div class="body">
This is my sample post, right here.
</div>
</div>
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
Wait a second...
<div class="entry">
<h1>{{post.title}}</h1>
<div class="body">
{{post.content}}
</div>
</div>

MVC

Model

Your data

View

What it looks like

Controller

Logic.

MVC in WordPress

Model

WordPress Database

View

The .php files inside the theme folder

Controller

The .php files inside the theme folder

So we get this

Um, this is bad ...

For WordPress developers

Bad for beginners

Bad for experts

... because my code is a mess

Bad for collaboration

Bad for WordPress

So let's fix it.

Symfony

Twig + WordPress

In normal WordPress

single.php

With WordPress Timber

single.php
single.twig

Seperation of Concerns

  1. Make a box
  2. Fill it with data
  3. Send it to the template

What can Twig do for you?

  • Variables
  • Simple Logic
  • Filters

Twig Features

Variables

  • Title
  • Permalink
  • ISBN (custom field)
  • <h1><?php echo get_the_title(); ?></h1>
  • <a href="<?php echo get_permalink(); ?>">My title</a>
  • The ISBN is: <?php echo get_post_meta(get_the_ID(), 'isbn', true); ?>
  • <h1>{{post.title}}</h1>
  • <a href="{{post.permalink}}"> My Title </a>
  • The ISBN is: {{post.isbn}}

Twig Features

Simple Logic

  • Conditionals
  • Loops
  • Includes

Twig Features

Filters

Twig Features

Template Extension

Who cares?

20%

Twig Docs

Timber Docs

Contribute to the code

Help with examples + documentation

Send me your sites!

Star Timber on GitHub!

http://bit.ly/timber-stl