Toolbox Twig functions

Estimated reading: 4 minutes

To speed up your workflow we have added a few functions to Toolbox. Some are meant for debugging and some are used to query fields more efficiently.

toolbox_get_fields( fields , [post_id] , [options] )

This Twig function gets the field or fields and returns them as an array

Parameters

  • fields(mixed) (required) fieldname (string) or fieldnames (array) that need to be returned
  • post_id (mixed) (optional) post_id where the value is saved. Defaults to current post
  • options (array) (optional) options for Meta Box

Usage

Get a value from the current post
{% set fields = toolbox_get_fields(['name','address','city']) %}
<h4>{{fields.name}}</h4>
<p>{{fields.address}}, {{fields.city}}</p>
Get a value from a specific post
{% set fields = toolbox_get_fields(['name','address','city'],123) %}
<h4>{{fields.name}}</h4>
<p>{{fields.address}}, {{fields.city}}</p>
Get a value from a user table (Meta Box only)

This examples shows how to get the values for post id 123, but from the custom table added with the mb-custom-table extension activated.

{% 
  set fields = toolbox_get_fields(
      ['name','address','city'],
      123,
      { 
        storage_type: 'custom_table', 
        table: 'custom_user_table' 
      }
    ) 
%}
<h4>{{fields.name}}</h4>
<p>{{fields.address}}, {{fields.city}}</p>

toolbox_get_field_objects( fields , [post_id] , [options] )

This Twig function returns the field_objects for the requested field(s). The field_object contain all essential data provided by the custom field solution and can be used to completely rebuild the output of the field.

A good example would be a checkbox field. The function toolbox_get_fields() will only return the selected options, without providing us with a way to list all options that are available on the backend.

The toolbox_get_field_objects() function however queries the field_object and provides you all data used to render both frontend and backend options.

Parameters

  • fields(mixed) (required) fieldname (string) or fieldnames (array) that need to be returned
  • post_id (mixed) (optional) post_id where the value is saved. Defaults to current post
  • options (array) (optional) options for Meta Box

Usage

Get a field_object from the current post
{% set fields = toolbox_get_field_objects( ['my_checkboxes'] ) %}
<h4>Selected value:</h4>
<p>{{ fields.my_checkboxes.value|join(', ') }}</p>
<h4>All available options</h4>
<p>{{ fields.my_checkboxes.choices|join( ', ' ) }}</p>

toolbox_dump( var )

This Twig function dumps the variable into your layout. It’s useful for debugging your twig values or finding the values on a fieldtype.

Parameters

  • var(mixed) (required) variable that you want to display

Usage

Output the current post variable
{{ toolbox_dump( post ) }}

toolbox_get_posts( options ) (DEPRICATED)

This is a Twig wrapper function for the WP get_posts() function. It can be used to query WP, using an object with key->value settings.

Parameters

  • options(object) (required) Query Args for getting the posts

Returns

The Twig function returns a WP Posts object.

Usage

{# depricated method #}
{% set new_posts = toolbox_get_posts( { post_type: 'post', cat: 'plugin', posts_per_page: 5 } ) %}
{% for item in new_posts %}
<h4>{{ item.post_title }}</h4>
{% endfor %}

Depricated

You can still use this method, but it is advised to use the tb proxy class to run a regular get_posts method instead. See below for details.

{# prefered method #}
{% set new_posts = tb.get_posts( { post_type: 'post', cat: 'plugin', posts_per_page: 5 } ) %}
{% for item in new_posts %}
<h4>{{item.post_title}}</h4>
{% endfor %}

PostQuery

A WP Posts Object is not the same as a Timber Posts Object. A WP Post Object only provides properties about the post, while a Timber Post Object also has properties that allow you to access the featured image, Terms connected to the post, preview and much much more.

In order to perform a WP Query and return Timber Post Objects, use the following method:

{# return Timber Post Objects #}
{% set new_posts = PostQuery( { post_type: 'post', cat: 'plugin', posts_per_page: 5 } ) %}
{% for item in new_posts %}
<img src="{{ item.thumbnail }}">
<h4>{{ item.title }}</h4>
<p>{{ item.terms|join( ',' ) }}</p>
{% endfor %}
Share this Doc

Toolbox Twig functions

Or copy link

CONTENTS