better-wordpress comes with handlebars-php as a dependancy, here's how to use and configure it.


Firstly, create a new template file in the tpl/ folder. For this example we’ll name the file home-title.tpl. For a complete guide on how to use Handlebars templating syntax, check out the handlebars-php repository.

<!-- tpl/home-title.tpl -->

Welcome to <span style="color:red">{{title}}</span>!


To render this we need to call the file and supply it with the data it needs. Since I only want to use this template on my home page, I’ll add this code to home.php:

<?php #home.php

echo site()->render("home-title",[
    "title" => get_bloginfo("name")

Recall we added a function in functions.php to expose our Site object to all the pages, and that’s what we’re using here.

Here’s what we see on the homepage now:

Wow! What a site!

Template File Extension

By default Handlebars templates are files that end with .tpl and exist inside the tpl/ folder in the theme root.

<?php #config.php
return [
    "handlebars" => [
        "template-extension" => "tpl"