Timber & Twig Interview Questions

Assessing knowledge of MVC patterns and Templating in WordPress.

Timber separates PHP logic from HTML markup using the Twig template engine. This is a standard in enterprise WordPress development to maintain clean, readable codebases.

1. Why use Timber instead of standard WordPress PHP templates?

Answer:

  • Separation of Concerns: Logic stays in PHP files, presentation stays in Twig files.
  • Cleaner Syntax: Twig is concise (`{{ post.title }}` vs `<?php the_title(); ?>`).
  • Security: Twig auto-escapes output by default, reducing XSS risks.
  • Inheritance: Twig's `extends` and `block` features allow for powerful layout inheritance (DRY).

2. How do you pass data from a PHP file to a Twig view?

Answer: You create a context array and pass it to `Timber::render()`.

// single.php
$context = Timber::context();
$context['post'] = Timber::get_post();
$context['custom_message'] = 'Hello World';

Timber::render('single.twig', $context);

3. Explain Template Inheritance in Twig.

Answer: It allows you to define a base skeleton (layout) and override specific blocks in child templates.

{# base.twig #}
<html>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>

{# single.twig #}
{% extends "base.twig" %}

{% block content %}
    <h1>{{ post.title }}</h1>
    <div>{{ post.content }}</div>
{% endblock %}

4. How do you add custom functionality (functions/filters) to Twig?

Answer: You hook into `timber/twig` filter.

add_filter('timber/twig', function($twig) {
    // Adding a function
    $twig->addFunction(new Twig\TwigFunction('my_function', function($text) {
        return 'Custom: ' . $text;
    }));

    // Adding a filter
    $twig->addFilter(new Twig\TwigFilter('price_format', function($price) {
        return '$' . number_format($price, 2);
    }));

    return $twig;
});

5. What is the difference between `Timber::get_post()` and `get_post()`?

Answer:

  • `get_post()` returns a standard `WP_Post` object (raw data).
  • `Timber::get_post()` returns a `Timber\Post` object. This object wraps the raw data and adds useful methods (e.g., `$post->thumbnail`, `$post->link`, `$post->terms`) that make accessing related data intuitive in Twig.

6. How do you handle Menus in Timber?

Answer:

In PHP, you fetch the menu object:

$context['menu'] = Timber::get_menu('primary-menu');

In Twig, you iterate over the items:

<ul>
    {% for item in menu.items %}
        <li class="{{ item.classes | join(' ') }}">
            <a href="{{ item.link }}">{{ item.title }}</a>
            {% if item.children %}
                <ul>
                    {% for child in item.children %}
                        <li><a href="{{ child.link }}">{{ child.title }}</a></li>
                    {% endfor %}
                </ul>
            {% endif %}
        </li>
    {% endfor %}
</ul>

7. What is the `Timber::get_widgets()` function?

Answer: It retrieves the HTML for a dynamic sidebar (widget area). This allows you to render widgets anywhere in your Twig template.

$context['sidebar'] = Timber::get_widgets('sidebar-1');
<aside>
    {{ sidebar }}
</aside>