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 #
Name | Return Type | Summary/Returns |
---|---|---|
array_to_object() | \stdClass | |
array_truncate() | array | |
convert_wp_object() | mixed | Converts a WP object (WP_Post, WP_Term) into its equivalent Timber class (Timber\Post, Timber\Term). Returns: Instance of equivalent Timber object, or the argument if no match is found |
deprecated() | void | Triggers a deprecation warning. |
doing_it_wrong() | Marks something as being incorrectly called. | |
error_log() | void | Output 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() | bool | Is the number even? Let's find out. |
isodd() | bool | Is the number odd? Let's find out. |
ob_function() | 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. |
osort() | void | Sorts object arrays by properties. |
pluck() | array | Plucks the values of a certain key from an array of objects |
start_timer() | float | For measuring time, this will start a timer. |
stop_timer() | string | For stopping time and getting the data. |
transient() | mixed | A utility for a one-stop shop for transients. |
warn() | void | Trigger a warning. |
wp_list_filter() | array | Filters a list of objects, based on a set of key => value arguments. |
Class Methods #
array_to_object() #
array_to_object( array $array )
Returns: \stdClass
Name | Type | Description |
---|---|---|
$array | array |
array_truncate() #
array_truncate( array $array, int $len )
Returns: array
Name | Type | Description |
---|---|---|
$array | array | |
$len | int |
convert_wp_object() #
Converts a WP object (WP_Post, WP_Term) into its equivalent Timber class (Timber\Post, Timber\Term).
If no match is found the function will return the initial argument.
convert_wp_object( mixed $obj )
Returns: mixed
Instance of equivalent Timber object, or the argument if no match is found
Name | Type | Description |
---|---|---|
$obj | mixed | WP Object to convert |
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
Name | Type | Description |
---|---|---|
$function | string | The name of the deprecated function/method. |
$replacement | string | The name of the function/method to use instead. |
$version | string | The version of Timber when the function was deprecated. |
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 )
Name | Type | Description |
---|---|---|
$function | string | The function that was called. |
$message | string | A message explaining what has been done incorrectly. |
$version | string | The version of Timber where the message was added. |
error_log() #
Output a value (string, array, object, etc.) to the error log
error_log( mixed $error )
Returns: void
get_object_by_property() #
get_object_by_property( array $array, string $key, mixed $value )
Returns: array|null
Name | Type | Description |
---|---|---|
$array | array | |
$key | string |
get_object_index_by_property() #
get_object_index_by_property( array $array, string $key, mixed $value )
Returns: bool|int
Name | Type | Description |
---|---|---|
$array | array | |
$key | string |
get_wp_title() #
get_wp_title( string $separator = ' ', string $seplocation = 'left' )
Returns: string
Name | Type | Description |
---|---|---|
$separator | string | |
$seplocation | string |
is_array_assoc() #
is_array_assoc( array $arr )
Returns: bool
Name | Type | Description |
---|---|---|
$arr | array |
is_true() #
is_true( mixed $value )
Returns: bool
iseven() #
Is the number even? Let's find out.
iseven( int $i )
Returns: bool
Name | Type | Description |
---|---|---|
$i | int | number to test. |
isodd() #
Is the number odd? Let's find out.
isodd( int $i )
Returns: bool
Name | Type | Description |
---|---|---|
$i | int | number to test. |
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
Name | Type | Description |
---|---|---|
$function | callable | |
$args | array |
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>
osort() #
Sorts object arrays by properties.
osort( array $array, string $prop )
Returns: void
Name | Type | Description |
---|---|---|
$array | array | The array of objects to sort. |
$prop | string | The property to sort by. |
pluck() #
Plucks the values of a certain key from an array of objects
pluck( array $array, string $key )
Returns: array
Name | Type | Description |
---|---|---|
$array | array | |
$key | string |
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
Name | Type | Description |
---|---|---|
$start | int |
PHP
$start = Timber\Helper::start_timer();
// do some stuff that takes awhile
echo Timber\Helper::stop_timer( $start );
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
Name | Type | Description |
---|---|---|
$slug | string | Unique identifier for transient |
$callback | callable | Callback that generates the data that's to be cached |
$transient_time | int | (optional) Expiration of transients in seconds |
$lock_timeout | int | (optional) How long (in seconds) to lock the transient to prevent race conditions |
$force | bool | (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);
warn() #
Trigger a warning.
warn( string $message )
Returns: void
Name | Type | Description |
---|---|---|
$message | string | The warning that you want to output. |
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
Name | Type | Description |
---|---|---|
$list | array | to filter. |
$args | string or array | to search for. |
$operator | string | to use (AND, NOT, OR). |