Timber Logo

You are reading the documentation for Timber v1.x. Switch to the documentation for Timber v2.x.

Timber\Timber

Timber Class.

Main class called Timber for this plugin.

PHP

<?php
$posts = Timber::get_posts();
$posts = Timber::get_posts('post_type = article')
$posts = Timber::get_posts(array('post_type' => 'article', 'category_name' => 'sports')); // uses wp_query format.
$posts = Timber::get_posts(array(23,24,35,67), 'InkwellArticle');
$context = Timber::context(); // returns wp favorites!
$context['posts'] = $posts;
Timber::render('index.twig', $context);

Overview #

Methods #

NameTypeReturns/Description
compilebool/stringThe returned output.
compile_stringbool/string
contextarray
fetchbool/stringThe returned output.
get_contextarray
get_paginationarraymixed
get_post\Timber\Post/boolTimber\Post object if a post was found, false if no post was found.
get_postsarray/bool/null
get_sidebarbool/string
get_sidebar_from_phpstring
get_sitesarray
get_term\Timber\Timber\Term/\Timber\WP_Error/null
get_termsmixed
get_widgetsstring
query_post\Timber\Post/array/bool/null
query_posts\Timber\PostCollection
renderbool/stringThe echoed output.
render_stringbool/string

Class Methods #

__construct #

__construct( )

returns: void


add_route #

DEPRECATED since 0.20.0 and will be removed in 1.1

add_route( string $route, \Timber\callable $callback, array $args=array() )

returns: void

Add route.

NameTypeDescription
$routestring
$callback\Timber\callable
$argsarray

compile #

compile( array/string $filenames, array $data=array(), bool/bool/int $expires=false, string $cache_mode="default", bool $via_render=false )

returns: bool/string The returned output.

Compile a Twig file.

Passes data to a Twig file and returns the output. If the template file doesn't exist it will throw a warning when WP_DEBUG is enabled.

NameTypeDescription
$filenamesarray/stringName of the Twig file to render. If this is an array of files, Timber will render the first file that exists.
$dataarrayOptional. An array of data to use in Twig template.
$expiresbool/bool/intOptional. In seconds. Use false to disable cache altogether. When passed an array, the first value is used for non-logged in visitors, the second for users. Default false.
$cache_modestringOptional. Any of the cache mode constants defined in TimberLoader.
$via_renderboolOptional. Whether to apply optional render or compile filters. Default false.

PHP

<?php
$data = array(
'firstname' => 'Jane',
'lastname' => 'Doe',
'email' => 'jane.doe@example.org',
);
$team_member = Timber::compile( 'team-member.twig', $data );

compile_string #

compile_string( string $string, array $data=array() )

returns: bool/string

Compile a string.

NameTypeDescription
$stringstringA string with Twig variables.
$dataarrayOptional. An array of data to use in Twig template.

PHP

<?php
$data = array(
'username' => 'Jane Doe',
);
$welcome = Timber::compile_string( 'Hi {{ username }}, I’m a string with a custom Twig variable', $data );

context #

context( )

returns: array

Alias for Timber::get_context() which is deprecated in 2.0.

This will allow us to update the starter theme to use the ::context() method and better prepare users for the upgrade (even if the details of what the method returns differs slightly).


fetch #

fetch( array/string $filenames, array $data=array(), bool/bool/int $expires=false, string $cache_mode="default" )

returns: bool/string The returned output.

Fetch function.

NameTypeDescription
$filenamesarray/stringName of the Twig file to render. If this is an array of files, Timber will render the first file that exists.
$dataarrayOptional. An array of data to use in Twig template.
$expiresbool/bool/intOptional. In seconds. Use false to disable cache altogether. When passed an array, the first value is used for non-logged in visitors, the second for users. Default false.
$cache_modestringOptional. Any of the cache mode constants defined in TimberLoader.

get_context #

get_context( )

returns: array

Get context.


get_pagination #

get_pagination( array $prefs=array() )

returns: array mixed

Get pagination.

NameTypeDescription
$prefsarray

get_post #

get_post( bool/mixed $query=false, string/string/array $PostClass="Timber\Post" )

returns: \Timber\Post/bool Timber\Post object if a post was found, false if no post was found.

Get a post by post ID or query (as a query string or an array of arguments). But it's also cool

NameTypeDescription
$querybool/mixedOptional. Post ID or query (as query string or an array of arguments for WP_Query). If a query is provided, only the first post of the result will be returned. Default false.
$PostClassstring/string/arrayOptional. Class to use to wrap the returned post object. Default 'Timber\Post'.

get_posts #

get_posts( bool/mixed $query=false, string/string/array $PostClass="Timber\Post", bool $return_collection=false )

returns: array/bool/null

Get posts.

NameTypeDescription
$querybool/mixed
$PostClassstring/string/array
$return_collectionbool

PHP

<?php
$posts = Timber::get_posts();
$posts = Timber::get_posts('post_type = article')
$posts = Timber::get_posts(array('post_type' => 'article', 'category_name' => 'sports')); // uses wp_query format.
$posts = Timber::get_posts('post_type=any', array('portfolio' => 'MyPortfolioClass', 'alert' => 'MyAlertClass')); //use a classmap for the $PostClass

get_sidebar #

get_sidebar( string $sidebar="sidebar.php", array $data=array() )

returns: bool/string

Get sidebar.

NameTypeDescription
$sidebarstring
$dataarray

get_sidebar_from_php #

get_sidebar_from_php( string $sidebar="", array $data )

returns: string

Get sidebar from PHP

NameTypeDescription
$sidebarstring
$dataarray

get_sites #

get_sites( bool/array/bool $blog_ids=false )

returns: array

Get sites.

NameTypeDescription
$blog_idsbool/array/bool

get_term #

get_term( int/\Timber\WP_Term/object $term, string $taxonomy="post_tag", string $TermClass="Timber\Term" )

returns: \Timber\Timber\Term/\Timber\WP_Error/null

Get term.

NameTypeDescription
$termint/\Timber\WP_Term/object
$taxonomystring
$TermClassstring

get_terms #

get_terms( mixed/string/array $args=null, array $maybe_args=array(), string $TermClass="Timber\Term" )

returns: mixed

Get terms.

NameTypeDescription
$argsmixed/string/array
$maybe_argsarray
$TermClassstring

get_widgets #

get_widgets( int/string $widget_id )

returns: string

Get widgets.

NameTypeDescription
$widget_idint/stringOptional. Index, name or ID of dynamic sidebar. Default 1.

init_constants #

init_constants( )

returns: void


query_post #

query_post( bool/mixed $query=false, string $PostClass="Timber\Post" )

returns: \Timber\Post/array/bool/null

Query post.

NameTypeDescription
$querybool/mixed
$PostClassstring

query_posts #

query_posts( bool/mixed $query=false, string $PostClass="Timber\Post" )

returns: \Timber\PostCollection

Query posts.

NameTypeDescription
$querybool/mixed
$PostClassstring

render #

render( array/string $filenames, array $data=array(), bool/bool/int $expires=false, string $cache_mode="default" )

returns: bool/string The echoed output.

Render function.

Passes data to a Twig file and echoes the output.

NameTypeDescription
$filenamesarray/stringName of the Twig file to render. If this is an array of files, Timber will render the first file that exists.
$dataarrayOptional. An array of data to use in Twig template.
$expiresbool/bool/intOptional. In seconds. Use false to disable cache altogether. When passed an array, the first value is used for non-logged in visitors, the second for users. Default false.
$cache_modestringOptional. Any of the cache mode constants defined in TimberLoader.

PHP

<?php
$context = Timber::context();
Timber::render( 'index.twig', $context );

render_string #

render_string( string $string, array $data=array() )

returns: bool/string

Render a string with Twig variables.

NameTypeDescription
$stringstringA string with Twig variables.
$dataarrayAn array of data to use in Twig template.

PHP

<?php
$data = array(
'username' => 'Jane Doe',
);
Timber::render_string( 'Hi {{ username }}, I’m a string with a custom Twig variable', $data );

init #

init( )

returns: void


test_compatibility #

test_compatibility( )

returns: void

Tests whether we can use Timber