Back to blog

Timber Experimentation

This is the first blog post, and I am experimenting with Timber and Twig.

To start using Timber, I needed to require Composer’s autoload file because this site hasn’t needed to use Composer until this point. So now that Timber is autoloaded by Composer, I can use Timber’s classes, and add it’s init method to initialise Timber for usage. I also changed Timber to look in the templates folder for Twig files, instead of it’s default views folder. All WP template files are kept in the custom templates folder anyway, so might as well keep it to the custom structure.

    functions.php
    <?php

// Autoload Composer dependencies.
require_once __DIR__ . '/../vendor/autoload.php';


/**
 * Testing Timber
 * https://timber.github.io/docs/v2/
 */

// Initialise Timber.
Timber\Timber::init();

// Set Timber to look in the templates folder for Twig files.
Timber::$dirname = 'templates';

Using Timber classes we can do normal WordPress-y and PHP stuff, in the page-blog template file, then render the Twig view with additional data, ie. the context which has all info about the current page including title, description, and custom fields.

Because we’re using a standard page for the blog, we need to setup the posts ourselves, which Timber’s get_posts is more or less a wrapper around WP’s WP_Query.

    page-blog.php
    <?php

$context = Timber::context();
$context["blog_posts"] = Timber::get_posts(
	[
	"post_type" => "post",
	]
);

Timber::render('views/blog.twig', $context);

The blog.twig file resides in the aforementioned views directory. This is essentially a HTML file, but allowing Twig syntax. The Twig {{ }} echo syntax works much like Blade’s syntax of the same nature, and allows for echoing PHP variables.

The view file has access to all data passed to it, and using Twig’s {% if } and {% for } syntax we can loop through the posts and render them on the page, like we would in standard PHP.

    blog.twig
    {# Twig #}

<section id="blog" class="blog pb-0">
    <div class="container">
        <h1>{{ post.title }}</h1>
        <div class="blog__intro mt-2">
            <p>{{ post.content }}</p>
        </div>

        {% if blog_posts is not empty %}
            <div class="blog__posts row my-5">
                {% for blog_post in blog_posts %}

                    <a href="{{ blog_post.link }}" class="post p-3 rounded w-25 p-3 btn btn-outline-primary cursor-interact">
                        <div class="post__date text-right">{{ blog_post.date }}</div>
                        <h4 class="post__title mt-3">{{ blog_post.title }}</h4>
                        <div class="post__excerpt mt-4"><p>{{ blog_post.excerpt }}</p></div>
                    </a>

                {% endfor %}
            </div>
        {% endif %}
    </div>
</section>

For the single post page, a single-post template file and single-post.twig view file is required. The ACF field values are accessed via Timber’s meta function to get the post’s meta data field via it’s slug (ACF field slug), which works like ACF’s get_field function. Using this, we loop through the repeater field, and access the subfields directly with dot notation. We can also conditionally add a code block if a toggle field is true.

    single-post.twig
    <section id="blog" class="blog {{ post.slug }} pb-0 my-5">
    <div class="container">
        <a class="back-to-blog" href="/blog"><i class="fa-solid fa-arrow-left-long mr-2"></i> Back to blog</a>
        <div class="post mt-5">

            <h1 class="post__title">{{ post.title }}</h1>
            <div class="post__date">{{ post.date }}</div>
            <div class="post__content mt-5">
                {% for item in post.meta("content") %}
                    <div class="post__content__item mb-5">
                        <div class="post__content__item__text">
                            {{ item.text }}
                        </div>

                        {# Only add code block if add_code field is true #}
                        {% if item.add_code === true %}
                            <pre class="post__content__item__code row row-cols-1 justify-content-end m-0">
                                <span class="filename position-absolute">{{ item.code_group.filename }}</span>
                                <code class="rounded">{{ item.code_group.code }}</code>
                            </pre>
                        {% endif %}
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
</section>

Learning Timber and Twig has taken significantly less time to grasp because of my extensive knowledge Laravel Blade – they seem to work in a similar fashion. While I don’t know the ins and outs of Timber or Twig just yet (though I don’t know everything about Blade!), I have whipped up this blog in no time. Timber seems very good at integrating itself into pre-existing PHP code, and can convert native PHP to Timber/Twig bit by bit.