Skip to content

Toolbox for Beaver Builder

Writing templates for fields

Mastering the Twig engine and explaining all the possibilities of Twig is beyond the scope of this documentation, but there are a few things you need to know when dealing with the template:

  • {# #} used to add comments;
  • {{ }} used to echo output to the render engine;
  • {% %} used to execute statements;
  • Each statement or expression needs to be put within it's own delimiters.

 

As any language the input needs to be valid commands. This means that starting a {% if %} {% endif %} sequence, it is imperative that the closing {% endif %} is there for the template to understand. When it's not, the script will fail and the page might crash.

Because we are designing the template on the Editor, the information that is returned is inserted using AJAX-calls. If the script fails, it will only result in that module not updating the preview. However, since you can also use Twig to call other PHP functions, if these don't Error gracefully your page might not render correctly.

Adding a template for a repeater field

Consider a repeater field with 2 sub fields, 'itemname' and 'description'. A repeater can be zero or several iterations of this combination.

Add a Toolbox Field Module into the layout and select the field type and field name from the drop-downs.

When working with a repeater field you need to iterate over the items inside the __field__ variable. An easy way is to do this is by using the for .. endfor loop. Item is a mere variablename and can be used to query each subitem.

So, inside the for...endfor loop we can get the itemname and description fields by using both {{item.itemname}} and {{item.description}}

resulting in the following layout:

Last modified: July 1st, 2019