Timber Logo

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

Timber\​Helper

As the name suggests these are helpers for Timber (and you!) when developing. You can find additional (mainly internally-focused helpers) in Timber\URLHelper.

Overview #

Methods #

NameReturn TypeSummary/Returns
array_to_object()\stdClass
array_truncate()array
deprecated()voidTriggers a deprecation warning.
doing_it_wrong()Marks something as being incorrectly called.
error_log()voidOutput a value (string, array, object, etc.) to the error log
get_object_by_property()array or null
get_object_index_by_property()bool or int
get_wp_title()string
is_array_assoc()bool
is_true()bool
iseven()boolIs the number even? Let's find out.
isodd()boolIs the number odd? Let's find out.
ob_function()stringCalls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template.
osort()voidSorts object arrays by properties.
pluck()arrayPlucks the values of a certain key from an array of objects
start_timer()floatFor measuring time, this will start a timer.
stop_timer()stringFor stopping time and getting the data.
transient()mixedA utility for a one-stop shop for transients.
warn()voidTrigger a warning.
wp_list_filter()arrayFilters a list of objects, based on a set of key => value arguments.

Class Methods #

transient() #

A utility for a one-stop shop for transients.

transient( string $slug, callable $callback, int $transient_time = '', int $lock_timeout = 5, bool $force = false )

Returns: mixed

NameTypeDescription
$slugstringUnique identifier for transient
$callbackcallableCallback that generates the data that's to be cached
$transient_timeint(optional) Expiration of transients in seconds
$lock_timeoutint(optional) How long (in seconds) to lock the transient to prevent race conditions
$forcebool(optional) Force callback to be executed when transient is locked

PHP

$context = Timber::context( [
'favorites' => Timber\Helper::transient( 'user-' . $uid . '-favorites' , function() use ( $uid ) {
// Some expensive query here that’s doing something you want to store to a transient.
return $favorites;
}, 600 ),
] );

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

start_timer() #

For measuring time, this will start a timer.

Returns: float


stop_timer() #

For stopping time and getting the data.

stop_timer( int $start )

Returns: string

NameTypeDescription
$startint

PHP

$start = Timber\Helper::start_timer();
// do some stuff that takes awhile
echo Timber\Helper::stop_timer( $start );

ob_function() #

Calls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template.

ob_function( callable $function, array $args = [null] )

Returns: string

NameTypeDescription
$functioncallable
$argsarray

PHP

function the_form() {
echo '<form action="form.php"><input type="text" /><input type="submit /></form>';
}

$context = Timber::context( [
'form' => Timber\Helper::ob_function( 'the_form' ),
] );

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

Twig

<h1>{{ post.title }}</h1>
{{ my_form }}

HTML

<h1>Apply to my contest!</h1>
<form action="form.php"><input type="text" /><input type="submit /></form>

error_log() #

Output a value (string, array, object, etc.) to the error log

error_log( mixed $error )

Returns: void

NameTypeDescription
$errormixedThe error that you want to error_log().

warn() #

Trigger a warning.

warn( string $message )

Returns: void

NameTypeDescription
$messagestringThe warning that you want to output.

doing_it_wrong() #

Marks something as being incorrectly called.

There is a hook 'doing_it_wrong_run' that will be called that can be used to get the backtrace up to what file and function called the deprecated function.

The current behavior is to trigger a user error if WP_DEBUG is true.

If you want to catch errors like these in tests, then add the @expectedIncorrectUsage tag. E.g.: "@expectedIncorrectUsage Timber::get_posts()".

see _doing_it_wrong()

since 2.0.0

doing_it_wrong( string $function, string $message, string $version )

NameTypeDescription
$functionstringThe function that was called.
$messagestringA message explaining what has been done incorrectly.
$versionstringThe version of Timber where the message was added.

deprecated() #

Triggers a deprecation warning.

If you want to catch errors like these in tests, then add the @expectedDeprecated tag to the DocBlock. E.g.: "@expectedDeprecated {{ TimberImage() }}".

see _deprecated_function()

deprecated( string $function, string $replacement, string $version )

Returns: void

NameTypeDescription
$functionstringThe name of the deprecated function/method.
$replacementstringThe name of the function/method to use instead.
$versionstringThe version of Timber when the function was deprecated.

get_wp_title() #

get_wp_title( string $separator = ' ', string $seplocation = 'left' )

Returns: string

NameTypeDescription
$separatorstring
$seplocationstring

osort() #

Sorts object arrays by properties.

osort( array $array, string $prop )

Returns: void

NameTypeDescription
$arrayarrayThe array of objects to sort.
$propstringThe property to sort by.

is_array_assoc() #

is_array_assoc( array $arr )

Returns: bool

NameTypeDescription
$arrarray

array_to_object() #

array_to_object( array $array )

Returns: \stdClass

NameTypeDescription
$arrayarray

get_object_index_by_property() #

get_object_index_by_property( array $array, string $key, mixed $value )

Returns: bool|int

NameTypeDescription
$arrayarray
$keystring
$valuemixed

get_object_by_property() #

get_object_by_property( array $array, string $key, mixed $value )

Returns: array|null

NameTypeDescription
$arrayarray
$keystring
$valuemixed

array_truncate() #

array_truncate( array $array, int $len )

Returns: array

NameTypeDescription
$arrayarray
$lenint

is_true() #

is_true( mixed $value )

Returns: bool

NameTypeDescription
$valuemixed

iseven() #

Is the number even? Let's find out.

iseven( int $i )

Returns: bool

NameTypeDescription
$iintnumber to test.

isodd() #

Is the number odd? Let's find out.

isodd( int $i )

Returns: bool

NameTypeDescription
$iintnumber to test.

pluck() #

Plucks the values of a certain key from an array of objects

pluck( array $array, string $key )

Returns: array

NameTypeDescription
$arrayarray
$keystring

wp_list_filter() #

Filters a list of objects, based on a set of key => value arguments.

Uses WordPress WP_List_Util's filter.

since 1.5.3

wp_list_filter( array $list, string|array $args, string $operator = 'AND' )

Returns: array

NameTypeDescription
$listarrayto filter.
$argsstring or arrayto search for.
$operatorstringto use (AND, NOT, OR).