Timber Logo

You are reading the documentation for Timber v1.x. Switch to the documentation for Timber v2.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 TimberURLHelper

Overview #

Methods #

NameTypeReturns/Description
ob_functionstring
start_timer\Timber\float
transientmixed

Class Methods #

array_to_object #

array_to_object( array $array )

returns: \stdClass

NameTypeDescription
$arrayarray

array_truncate #

array_truncate( array $array, int $len )

returns: array

NameTypeDescription
$arrayarray
$lenint

close_tags #

DEPRECATED since 1.2.0

close_tags( string $html )

returns: string

NameTypeDescription
$htmlstring

convert_wp_object #

convert_wp_object( \Timber\mix $obj )

returns: \Timber\mix Instance of equivalent Timber object, or the argument if no match is found

Converts a WP object (WP_Post, WP_Term) into his equivalent Timber class (Timber\Post, Timber\Term).

If no match is found the function will return the inital argument.

NameTypeDescription
$obj\Timber\mixWP Object

error_log #

error_log( mixed $error )

returns: void

NameTypeDescription
$errormixed

filter_array #

filter_array( array $list, \Timber\callback/string/array $arrow, string $operator="AND" )

returns: array

Filters a list of objects, based on a set of key => value arguments. Uses native Twig Filter.

NameTypeDescription
$listarrayto filter.
$arrow\Timber\callback/string/arrayfunction used for filtering, string or array for backward compatibility.
$operatorstringto use (AND, NOT, OR). For backward compatibility.

function_wrapper #

DEPRECATED since 1.3.0

function_wrapper( mixed $function_name, array $defaults=array(), bool $return_output_buffer=false )

returns: \Timber\FunctionWrapper/mixed

NameTypeDescription
$function_namemixedString or array( $class( string
$defaultsarray
$return_output_bufferboolOptional. Return function output instead of return value. Default false.

get_comment_form #

DEPRECATED 0.21.8 use {{ function('comment_form') }} instead

get_comment_form( mixed/int $post_id=null, array $args=array() )

returns: string

Gets the comment form for use on a single article page

NameTypeDescription
$post_idmixed/intwhich post_id should the form be tied to?
$argsarray

get_current_url #

get_current_url( )

returns: string


get_object_by_property #

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

returns: array/null

NameTypeDescription
$arrayarray
$keystring
$valuemixed

get_object_index_by_property #

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

returns: bool/int

NameTypeDescription
$arrayarray
$keystring
$valuemixed

get_wp_title #

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

returns: string

NameTypeDescription
$separatorstring
$seplocationstring

is_array_assoc #

is_array_assoc( array $arr )

returns: bool

NameTypeDescription
$arrarray

is_true #

is_true( mixed $value )

returns: bool

NameTypeDescription
$valuemixed

iseven #

iseven( int $i )

returns: bool

NameTypeDescription
$iint

isodd #

isodd( int $i )

returns: bool

NameTypeDescription
$iint

ob_function #

ob_function( \Timber\callback $function, array $args=array() )

returns: string

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.

NameTypeDescription
$function\Timber\callback
$argsarray

PHP

<?php
function the_form() {
echo '<form action="form.php"><input type="text" /><input type="submit /></form>';
}
$context = Timber::context();
$context['post'] = new Timber\Post();
$context['my_form'] = TimberHelper::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>

osort #

osort( array $array, string $prop )

returns: void

NameTypeDescription
$arrayarray
$propstring

DEPRECATED since 1.1.2

paginate_links( array $args=array() )

returns: array

NameTypeDescription
$argsarray

pluck #

pluck( array $array, string $key )

returns: void

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

NameTypeDescription
$arrayarray
$keystring

start_timer #

start_timer( )

returns: \Timber\float

For measuring time, this will start a timer


stop_timer #

stop_timer( int $start )

returns: string

For stopping time and getting the data

NameTypeDescription
$startint

PHP

<?php
$start = TimberHelper::start_timer();
// do some stuff that takes awhile
echo TimberHelper::stop_timer( $start );

transient #

transient( string $slug, \Timber\callable $callback, \Timber\integer $transient_time, mixed/\Timber\integer $lock_timeout=5, bool/boolean $force=false )

returns: mixed

A utility for a one-stop shop for Transients

NameTypeDescription
$slugstringUnique identifier for transient
$callback\Timber\callableCallback that generates the data that's to be cached
$transient_time\Timber\integer(optional) Expiration of transients in seconds
$lock_timeoutmixed/\Timber\integer(optional) How long (in seconds) to lock the transient to prevent race conditions
$forcebool/boolean(optional) Force callback to be executed when transient is locked

PHP

<?php
$context = Timber::context();
$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);

trim_words #

DEPRECATED since 1.2.0

trim_words( string $text, mixed/int $num_words=55, mixed/string/null/false $more=null, string $allowed_tags="p a span b i br blockquote" )

returns: string

NameTypeDescription
$textstring
$num_wordsmixed/int
$moremixed/string/null/falsetext to appear in "Read more...". Null to use default, false to hide
$allowed_tagsstring

warn #

warn( string $message )

returns: boolean

NameTypeDescription
$messagestringthat you want to output

wp_list_filter #

wp_list_filter( array $list, string/array $args, string $operator="AND" )

returns: array

Filters a list of objects, based on a set of key => value arguments. Uses WordPress WP_List_Util's filter.

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

handle_transient_locking #

handle_transient_locking( string $slug, \Timber\callable $callback, \Timber\integer $transient_time, \Timber\integer $lock_timeout, boolean $force, boolean $enable_transients )

returns: void

Does the dirty work of locking the transient, running the callback and unlocking

NameTypeDescription
$slugstring
$callback\Timber\callable
$transient_time\Timber\integerExpiration of transients in seconds
$lock_timeout\Timber\integerHow long (in seconds) to lock the transient to prevent race conditions
$forcebooleanForce callback to be executed when transient is locked
$enable_transientsbooleanForce callback to be executed when transient is locked