rickibarnes
rickibarnes

Reputation: 327

Extending Timber

I'm using Timber for the first time, and I find that it is hard to convert my pure PHP thinking to this templating model.

What I'm confused about at the moment is using a date which is an input field, not the post date.

So I've got a repeater field from ACF. It has three sub-fields: release_date, document_file, and document_title. My release date format is YYYY-MM-DD.

As you might have guessed, this repeater field is output as a list of PDFs.

So far so good, but then I want some advanced functionality - namely, to have the PDFs display by release_date by default. I also want to be able to filter by the year, i.e. - if you click "2015", it would only show the documents from that year.

I know exactly how I would do this in straight WordPress, but I'm pretty confused making it on Timber. I've been trying to do it using a custom filter, but I have felt what I actually want is a custom class?

In addition, when I installed Timber it didn't come with the starter theme, so I did a search and downloaded one from GitHub. I've got a feeling this is a legacy version though, because the file structure and syntax doesn't seem to match the documentation.

Downloaded from here: https://github.com/timber/starter-theme

But for example this code in the starter theme functions.php:

function add_to_twig( $twig ) {
    /* this is where you can add your own functions to twig */
    $twig->addExtension( new Twig_Extension_StringLoader() );
    $twig->addFilter('split_date', new Twig_SimpleFilter('split_date', array($this, 'split_date')));
    return $twig;
}

Doesn't quite match the syntax in https://github.com/timber/timber/wiki/Extending-Timber under "Adding to Twig".

Upvotes: 1

Views: 885

Answers (1)

Adam Genshaft
Adam Genshaft

Reputation: 824

Here are a few helper functions to make it easy for me to extend timber.

function add_context_var( $key, $var ) {
    add_filter( 'timber_context', function ( $context ) use ( $key, $var ) {
        $context[ $key ] = $var;

        return $context;
    } );
}

function add_context_func( $key, $callback ) {
    add_filter( 'timber/twig', function ( $twig ) use ( $key, $callback ) {
        $twig->addFunction( new \Twig_SimpleFunction( $key, $callback ) );

        return $twig;
    } );
}

function add_to_context( $key, $val ) {
    if ( is_callable( $val ) ) {
        add_context_func( $key, $val );
    } else {
        add_context_var( $key, $val );
    }
}

function add_to_context_filter( $key, $callback ) {
    add_filter( 'get_twig', function ( $twig ) use ( $key, $callback ) {
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter( new Twig_SimpleFilter( $key, $callback ) );

        return $twig;
    } );
}

//php file
add_to_context("blue", "this key is blue")
add_to_context("red", function($extra = ""){
  return "this key is red and it has $extra";
})
add_to_context_filter( "relative_link", function ( $content ) {
    return str_replace( "http://", "//", $content );;
} );

//twig file
color: {{ blue }}
color: {{ red("a function parameter") }}
github: {{ "http://github.com/"|relative_link }}

//output html
color: this key is blue
color: this key is red and it has function parameter
github: //github.com/

Upvotes: 2

Related Questions