Skip to main content

Using Advanced Functionality in PageWorkers Templates

Updated over 10 months ago

📘 This article explains how to use functions and conditionals to define PageWorkers templates more granularly. PageWorkers is part of Botify's Activation Suite, available as an option with all Botify plans.

Overview

Some of the advanced templating features available in PageWorkers templating are described here. For the full list of templating features, refer to the Nunjucks documentation.

Applying Functions to Variables

Including functions in your templates can modify the variables' existing state or return a value, such as changing capitalization or returning a list count, as in the following examples:

##{{product | capitalize}}
##{{product_list | length}}

To include a function with a variable, append the pipe | symbol and the function's name after the variable in the template.

Formatting Text

This section provides functions that enable text formatting. Ensure the variable you use is always a string, not a number or anything else.

Function

Description

Example

Result

Lower

Lower returns the value of the string variable in lowercase.

##{{ myVar | lower }}

If myVar equals “This is Botify”, then the function returns “this is botify”.

Upper

Upper returns the value of the string variable in uppercase.

##{{ myVar | upper }}

If myVar equals “This is Botify”, then the function returns “THIS IS BOTIFY”.

Title

Title returns the value of the string variable with an uppercase at the start of each word.

##{{ myVar | title }}

If myVar equals “This is Botify”, then the function returns “This Is Botify”.

Capitalize

Capitalize returns the value of the string variable with an uppercase at the beginning of the first word and lowercase for all the following letters of your variable (including those that were originally in uppercase).

##{{ myVar | capitalize }}

If myVar equals “this is botify. please do capitalize”, then the function returns “This is botify. please do capitalize”.

Truncate

The truncate function shortens words or sentences by the number of characters identified in the first parameter.

##{{ myVar | truncate(6) }}

If myVar equals “This is Botify”, then the function

returns “This i…”.

##{{ myVar | truncate(6, true) }}

If myVar equals “This is Botify”, then the function

returns "This i...".

##{{ myVar | truncate(6, false) }}

If myVar equals “This is Botify”, then the function “This…” (false indicates that if a word is cut by truncate and it should be excluded).

##{{ myVar | truncate(6, true, ".") }}

If myVar equals “This is Botify”, then the function returns “This i.” (replaces “…” ellipsis with “.”).

Formatting Numbers

This section provides functions that enable numerical formatting and arithmetic operations.

Function

Description

Example

Result

Round

There are various options for rounding numbers, as shown in these examples if myVar = 4.49.​​​​​

##{{ myVar | round }}

Returns 4

##{{ myVar | round(1)}}

Returns 4.5

##{{ myVar | round(0, "ceil") }}

Returns 5

##{{ myVar | round(1, "floor")}}

Returns 4.4

Arithmetic operations

You can add, multiply, and divide variables. For example, if myVar = 4 and myVar2 = 5

##{{ myVar * myVar2 }}

Returns 20

Convert a string to a number

When extracting a number from a page, you might extract a string, not a number; however, you need to convert it to an integer or a float if you want to perform arithmetic operations. For example, extracting “4” in “4 items selected”.

##{{ "4" + 4 }}

Returns 44

##{{ ("4" | int) + 4}}

Returns 8

Replacing Words and Numbers

This section provides functions that enable you to replace text, numbers, and to use regular expressions with replace functions.

Function

Description

Example

Result

Replacing strings

If myVar = “PageWorkers is great, super great”

##{{ myVar | replace("great", "awesome") }}

Returns “Pageworkers is awesome, super awesome”.

Replacing numbers

If myVar = 2020

##{{ myVar | replace(2, 1) }}

Returns 1010

Replacing with regex

In this example, only the first word is changed.

If myVar = “Activation is awesome”

##{{ myVar | replace(r/(\w+)/, "Pageworkers") }}

The regex is encapsulated between “r/” and “/”.

Returns “Pageworkers is awesome”.

In this example, all words are changed.

If myVar = “Activation is awesome”

##{{ myVar | replace(r/(\w+)/g, "Pageworkers") }}

The regex is encapsulated between “r/” and “/g”.

Returns “Pageworkers Pageworkers Pageworkers”.

Combining Functions

You can combine functions on a variable, such as replacing parts of a string and converting it to uppercase.

For example, if myVar = “Activation is great!”, then:

##{{ myVar | replace("Activation", "Pageworkers") | upper }}

Returns “PAGEWORKERS IS GREAT!”.

When combining functions, it is important to pay attention to the order. For example, if myVar = “Activation is great!”, then:

##{{ myVar | upper | replace("Activation", "Pageworkers") }}
  • myVar becomes “ACTIVATION IS GREAT!”

  • Since the Replace function is case-sensitive, “Activation” does not equal “ACTIVATION”; therefore, ACTIVATION is not replaced by Pageworkers.

Using Conditionals and Logic in Templates

You can use conditions and logic in your templates to create intelligent rules.

If Condition

You can construct conditional statements in your templates to define one variable to be used if the first condition is met and another variable to be used if the second condition is met. Identify If/Else clauses within brackets and %, as shown below with myVar = 30:

{% if myvar >= 50 %}
"Price is above $50"
{% else %}
"Price is below $50"
{% endif %}

Returns “Price is below $50”.

This conditional template works this way:

  • Checks if myVar >= 50.

  • If the condition is true, it returns “Price is above $50”.

  • If it is false, it returns “Price is below $50”.

When adding an If condition, you must ensure you end it with {% endif %}.

You can also use {% elif %} to add another conditional result in your if statement:

{% if myVar >= 50 %}
"Price is above $50"
{% elif myVar >= 30 %}
"Price is between $30 and $50"
{% else %}
"Price is below $50"
{% endif %}

Returns “Price is between $30 and $50”.

This conditional template works this way:

  • Checks if myVar >= 50.

  • If the condition is true, it returns “Price is above $50”.

  • If it is false, it checks if myVar is between 30 and 50.

  • If this Elif condition is true, it returns “Price is between $30 and $50”.

  • If it is false, it returns “Price is below $50”.

You can also have no {% else %} statement, for instance:

{% if myVar >= 50 %}
"Price is above $50"
{% endif %}

Returns nothing (myVar = 30 so it does not match the condition).

Comparing Values

When comparing values, remember:

  • To check equality, use “==”. For instance, myVar == 3 returns true if myVar equals 3; otherwise, it is false.

  • To check superiority, use “>” (exclusively greater than) or “>=” (greater than or equal).

  • To check inferiority, use “<“ (exclusively lower than) or “<=” (lower than or equal).

Logical operators

When testing conditions, you can also combine them using or/and/not.

{% if myVar >= 50 and myVar <= 80 %}
"Price is above $50 and below $80"
{% endif %}

If myVar = 60, the template returns “Price is above $50 and below $80”.

You can use the same syntax with “or” and use “not”. For instance:

{% if not myVar %}
"myVar doesn't exist, or equals 0, or equals null, or is an empty string"
{% endif %}

When using Not, ensure you put parenthesis around the statement you want to evaluate.

For instance, if myVar = 2:

{ not myVar > 2 }

Returns false because it reads as not myVar first (which equals false) and then checks false > 2, which is false. Instead, do this:

{ not (myVar > 2) }

Which returns true because myVar > 2 is false, so not (myVar > 2) is true.

Reminder for “And” logic:

  • True and True return True

  • False and True return False

  • True and False return False

  • False and False return False

Reminder for “Or” logic:

  • True or False return True

  • False or True return True

  • True or True return True

  • False or False return False

Check if a Variable is Defined (Contains a Value)

In the templates, some values are considered equivalent to False:

  • ““ (empty string)

  • 0

  • null

All other values are considered equivalent to True.

This means you can check if a variable is not empty with the following:

{% if myVar %}
"myVar is not empty, null or equals 0"
{% else %}
'myVar is empty, null or equals O'
{% endif %}

Using Regular Expressions in Templates

You can use Regex in your templates; however, regex may make your templates more complicated than needed. In the template syntax, regex must be enclosed between “r/” and a “/,” which can be followed by the options below:

  • g: apply globally

  • i: case insensitive (beware, by default, regex are case sensitive)

  • m: multiline

  • y: sticky

You can test the presence of a word/expression using the following syntax:

{% set regExp = r/^Pageworkers.*/g %}
{% if regExp.test('Pageworkers introduction') %}
The sentence starts with Pageworkers
{% endif %}

You can also extract content using a regexp:

{% set regExp = r/(?<=color is )[^and]*/ %}
{% if regExp.test(myVar) %}
##{{ myVar.match(regExp) }}
{% endif %}

The template above checks the sentence contains a string like “color is XXX and” and extracts “XXX”. For instance, if myVar = “The color is light blue and the size is XS”, the template above returns “light blue”.

Testing Advanced Functionality

To ensure the advanced templating functions produce the desired results, we strongly recommend you use the Preview function when creating templates.

Did this answer your question?