Last modified: August 22, 2025
In order to expand the logic and functionality of your templates, HubL supports several key operators and expression tests. The operators allow you to execute math functions, make comparisons, complicate template logic, and alter what markup renders. In addition, this article contains a comprehensive list of expression tests that can be used in HubL.
Operators
Operators are symbols that tell the HubL compiler to execute various operations that result in the final output. Operators are placed between operands in order to relate the two values, whether for executing math functions, making comparisons, or implementing boolean expressions.
Math
Standard math operators can be used to calculate values in the context of a template.Symbol | Description |
---|---|
+ | Adds two objects together, generally number values. To concatenate strings or lists, you should use the ~ operator instead. |
- | Subtracts one number from another. |
/ | Divides numbers. |
% | Returns the remainder from dividing numbers. |
// | Divide two numbers and return the truncated integer result. For example, {{ 20 // 7 }} is 2 . |
* | Multiplies numbers. |
** | Raise the left operand to the power of the right operand. |
Comparison
Comparison operators can be used to evaluate values for template logic. You can see some examples of comparison operators being used in if statements.Symbol | shorthand | Description |
---|---|---|
== | eq | Equal to. Evaluates to true if the two objects have equal values. |
!= | ne | Not equal to. Evaluates to true if the two objects are not equal. |
> | gt | Greater than. Evaluates to true if the left operand value is greater than the right operand. |
>= | gte | Greater than or equal to. Evaluates to true if the left operand is greater or equal to the right operand. |
< | lt | Less than. Evaluates to true if the left operand is lower than the right operand. |
<= | lte | Less than or equal to. Evaluates to true if the left operand is lower or equal to the right operand. |
The shorthand version of the comparison operators are usable in HubL filters that involve testing an expression such as
|selectattr()
.Logical
Logical operators allow you to implement boolean expressions, as well as combine multiple expressions into single statements.Symbol | Description |
---|---|
and | Returns true if both the left and right operand are truthy. Otherwise, returns false . This operator does not behave like the and operator in Python or the && operator in JavaScript. Learn more about using and operators below. |
or | Returns the first operand if it is truthy. Otherwise, returns the second operand. This operator is equivalent to or in Python and || in JavaScript. Learn more about using or operators below. |
is | Joins two operands for an affirmative statement. |
not | Negates a statement, in conjunction with is . |
(expr) | Group an expression for the order of operations. For example, (10 - 2) * variable . |
? | The ternary operator can be used to quickly write conditional logic. Accepts 3 arguments: expression, true condition, false condition. Evaluates an expression and returns the corresponding condition. |
or
operator behaves like the or
operator in Python and the ||
operator in JavaScript. It will return the first operand if the expression evaluates as true, otherwise it will return the second operand. A common use case for the or
operator is setting a fallback value when a variable value isn’t defined.
and
operator behaves differently than the and
operator in Python and the &&
operator in JavaScript. In HubL, and
will always return a boolean value: when the expression evaluates as true, true
is returned, otherwise it will return false
. The Python and JavaScript operators, on the other hand, will return an operand value based on whether the statement evaluates as true or false.
[]
) and empty dicts ({}
) are considered falsy. This is equivalent to the behavior in Python, but different from JavaScript, where []
and {}
are truthy.
Other HubL operators
Below are other important HubL operators that can be used to perform various tasks.Symbol | Description |
---|---|
in | Checks to see if a value is in a sequence. |
is | Performs an expression test. |
| | Applies a filter. |
~ | Concatenates values. |
Expression tests
Expression tests are various boolean conditions that can be evaluated by using logical operators.boolean
Tests whether the object is boolean (in a strict sense, not in its ability to evaluate to a truthy expression).containing
Tests whether a list variable has a value in it.containingall
Tests if a list variable contains all of the values of another list.defined
Tests whether a variable is defined within the context of the template. While you can use this expression test, writing an if statement without any operators will default to checking whether or not the variable is defined. In the example below, a color module’s color parameter is tested. If the color parameter had no value, the template would render a default black background color. If it is defined, it renders the background color set by the user.divisibleby
Tests whether an object is divisible by another number. For example, below a for loop is created that iterates through a list of types of animals. Each type of animal gets printed in a div, and every 5th div has different inline styling applied (width:100%). This concept could be applied to a blog where different markup is rendered for a certain pattern of posts. To learn more about for loops and loop.index, check out this article.equalto
Tests whether a variable’s value is equal to a constant or another variable. You can also use the operator==
to do the same test.
In the example below, the width of the blog posts is adjusted based on the total number of posts in the loop. The example output assumes there were 4 posts in the blog.
even
Tests whether a numeric variable is an even number. The example below shows a simplified blog listing loop, where if the current iteration of the loop is even, a class ofeven-post
is assigned to the post item div. Otherwise, a class of odd-post
is assigned.
float
Tests whether a numeric variable is a floating-point number.integer
Tests whether a variable is an integer.iterable
Tests whether a variable can be looped through. This example checks a variable calledjobs
to see if it can be iterated through. Since the variable contains a list of jobs, the if statement would evaluate to true
, and the loop would run. If the variable had contained a single value, the if statement would print that value with different markup instead. Learn more about for loops.
lower
Tests whether a string is lowercase. The example below uses an unless statement and a lower filter to ensure that a string of text entered into a text module is always lowercase.mapping
Tests whether an object is a dict (dictionary). The example below is checking to see if the contact object is a dictionary.none
Tests whether a variable has anull
value.
number
Tests whether the value of a variable is a number. The example below checks a variable to see whether or not it is a variable, and if so it converts it into millions.odd
Tests whether a numeric variable is an odd number. Below is the same example as the inverse even expression test previously described.sameas
Tests whether or not two variables have the same value. The example below sets two variables and then checks to see whether or not they are the same.sequence
Similar to the iterable test, this expression test checks whether a variable is a sequence. The example below tests whether a variable is a sequence, then iterates through that sequence of musical genres.string
Tests whether the value stored in a variable is text. The example below checks whether a variable is a string, and if so it applies a title filter to change the capitalization.string_containing
Tests whether a provided substring is contained within another string. This expression test is used in conjunction with theis
operator.
string_startingwith
Tests whether a string starts with a particular string. It is used in conjunction with theis
operator.
truthy
Tests whether an expression evaluates toTrue
.
The example below uses a boolean checkbox module to display an alert message.
undefined
Tests whether a variable is undefined in the context of the template. This test is different from thenone
expression test in that undefined will be true
when the variable is present but has no value; whereas, none will be true
when the variable has a null value.
The example below checks a template for the existence of the variable "my_var"
.
upper
Tests whether a string is all uppercase. Below is an inverse example of thelower
expression test above.