Toolbox Twig Result

Estimated reading: 3 minutes

The Toolbox Twig Result rule allows you to show/hide nodes by resolving a Twig Template. The output of the Twig Template can be passed through the operator to do that.

toolbox_docs-logic03

Using the Twig Template CPT provided by Toolbox, you can perform any number of checks needed and based on the result output a value or, alternatively, return empty.

Returning an empty output will logically compare to false, thereby hiding the node. Returning anything at all will compare to true, thereby showing the node.

Changing the operator to ‘equals‘ or ‘does not equal‘ will enable you to compare the Twig Template results to the compare value.

An example

Let’s assume we want to show a node only when the visitor is requesting the page on business days between 9 AM and 5 PM. We can use the following Twig Template to do that:

{# 
    check for business day (N) and business hour (G) 
    https://www.w3schools.com/php/func_date_date_format.asp
#}
{{ 
    date("now")|date('N') in [1,2,3,4,5] 
        and 
    date("now")|date('G') in [9,10,11,12,13,14,15,16,17]
}}

The template checks the current date date("now") to get the day of the week using the |date('N') filter. It checks if the returning day is in the array [1,2,3,4,5] (which are monday, tuesday, wednesday, thursday and friday respectively).

It also checks if the current hour matches with the business hours using the same priciple. If BOTH checks are true, it will echo a “1” as the output. If not, the results remain empty.

Add the template to the Twig Templates CPT as a new template. Make sure to take a note of the slug after the save, and write it down or copy it to your clipboard.

toolbox_docs-logic05

Now you can use that Twig Template in the Conditional Logic rules.

toolbox_docs-logic04

Why should you use this?

By now you must now that the Twig Templates translate to easy to read and manage PHP code mixed with HTML. That means that you could also have done this using a filter and callback in your functions.php.

The upside of using Toolbox Twig Result is that you will always see that you have the rule applied, whereas when you use a filter from functions.php that’s a little harder to spot. You will know where to check the code and are able to do so from withing your dashboard.

The negative is also usable

You can also quickly apply the same rule as a negative on another node. Let’s take this same example. Let’s assume we used it to show our opening hours or phone number so they can call it right away.

But now we want to add a heading only when closed:

We’re not open, you can call us back Mondays to Fridays between 9 AM and 5 PM!

Simply add the same rule, but this time se the operator to ‘is not set’, so that when we are within business hours the message will hide instead of show.

toolbox_docs-logic06

When rules change, it changes everywhere

Another advantage is that the rules apply the same to wherever you’ve used them. So, if you would ever need to change the rule it will apply the same everywhere it is used. Even across multiple layouts.

Share this Doc

Toolbox Twig Result

Or copy link

CONTENTS