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 .
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. |
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. |
|selectattr()
.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.
Symbol | Description |
---|---|
in | Checks to see if a value is in a sequence. |
is | Performs an expression test. |
| | Applies a filter. |
~ | Concatenates values. |
==
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-post
is assigned to the post item div. Otherwise, a class of odd-post
is assigned.
jobs
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.
null
value.
is
operator.
is
operator.
True
.
The example below uses a boolean checkbox module to display an alert message.
none
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”.
lower
expression test above.