Debugging

Enable debugging

To use debugging, the constant WP_DEBUG needs to be set to true.

wp-config.php

define( 'WP_DEBUG', true );

Using Twig’s native functions

Twig includes a dump function that can output the properties of an object.

Twig

{{ dump(post) }}

Which will give you:

You can also dump everything sent to your template (all the contents of $context that were passed to the Twig file) via:

{{ dump() }}

This will give you something like:

Formatted output

For a highlighted output like you see it above, you need to have xDebug enabled in your local development environment. With some environments like MAMP, enabling it is as easy as ticking a checkbox and restarting the server. Other times, it might be more complex.

An easier solution is to use the Timber Dump Extension, which will make use of the Symfony VarDumper component to generate output like this when using {{ dump() }} in Twig:

It also works in PHP. Instead of using var_dump or print_r, you will use dump() as well:

dump( $post );

Commented Twig includes

Sometimes it’s difficult to know which Twig file generated a certain output. Thankfully, there’s the Timber Commented Include extension. It will generate HTML comments that indicate where a template starts and where it ends:

<!-- Begin output of "partials/navigation.twig" -->
<nav class="navigation">...</nav>
<!-- / End output of "partials/navigation.twig" -->).

The extension is only active when WP_DEBUG is set to true.

Set xDebug breakpoints in Twig

Certain IDEs allow you to set breakpoints in your PHP code. To do that in Twig, you can use the AjglBreakpointTwigExtension extension, that allows you to set breakpoints and inspect environment and context variables.

Install it as a dev-dependency:

composer require ajgl/breakpoint-twig-extension --dev

And then add it to Timber’s Twig environment:

functions.php

add_filter( 'timber/twig', function( Twig_Environment $twig ) {
    if ( defined( 'WP_DEBUG' ) && WP_DEBUG
        && class_exists( 'Ajgl\Twig\Extension\BreakpointExtension' )
    ) {
        $twig->addExtension( new Ajgl\Twig\Extension\BreakpointExtension() );
    }

    return $twig;
} );

Finally, you can set a breakpoint anywhere in your Twig file:

<nav>
    {{ breakpoint() }}
</nav>

Using Timber Debug Bar plugin

There’s a Timber add-on for the WordPress debug bar.
Warning: this currently requires PHP 5.4.

Using (Legacy) Timber Filters

You can also use some quick filters on an object. These are legacy and will be removed in favor of using Twig’s built-in functionality. However, these do not require that WP_DEBUG be turned on.

Passes the variable to PHP’s print_r function.

{{ post|print_r }}

get_class

This filter answers the question: What type of object am I working with? It passes a variable to PHP’s get_class function.

{{ post|get_class }}

It will output something like TimberPost or your custom wrapper object.