Operators & Expression Tests
Standard math operators can be used to calculate values in the context of a template.
|+||Adds two objects together. You will generally use this for the addition of numbers. If you are trying to concatenate strings of lists, you should use
|-||Subtracts one number from another.|
|%||Returns the remainder from dividing numbers|
|//||Divide two numbers and return the truncated integer result. Example:
|**||Raise the left operand to the power of the right operand|
|==||eq||Equal to. Evaluates to true if two objects are equal.|
|!=||ne||Not equal to. Evaluates to true if two objects are not equal.|
|>||gt||Greater than. Evaluates to true if the left-hand side is greater than the right-hand side.|
|>=||gte||Greater than or equal to. Evaluates to true if the left-hand side is greater or equal to the right-hand side.|
|<||lt||Less than. Evaluates to true if the left-hand side is lower than the right-hand side.|
|<=||lte||Less than or equal to. Evaluates to true if the left-hand side is lower or equal to the right-hand side.|
The shorthand version of the comparison operators are usable in filters that involve testing an expression such as
Logical operators allow you to combine multiple expressions into single statements.
|and||Return true if the left and the right operand are true.|
|or||Return true if the left or the right operand is true.|
|not||Negates a statement and is used in conjunction with is. See examples below.|
|(expr)||Group an expression for the order of operations. For example, (10 - 2) * variable.|
|?:||The ternary operator accepts 3 arguments (expression, true condition, false condition). Evaluates an expression and returns the corresponding condition.|
Below are other important HubL operators that can be used to perform various tasks.
Expression tests are various boolean conditions that can be evaluated by using logical operators.
containingall expression test checks if a list variable contains all of the values of another list.
The defined expression test checks to see 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.
The expression test divisibleby can be used to test 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.
The equalto expression test checks to see if 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.
The even expression test checks to see 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 of even-post is assigned to the post item div. Otherwise, a class of odd-post is assigned.
Checks to see whether a variable is iterable and can be looped through.
This example checks a variable called "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.
The lower expression test evaluates to true when 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.
The mapping expression test checks to see whether or not an object is a dict (dictionary).
The example below is checking to see if the contact object is a dictionary, in which case it is.
The none expression test checks to see whether a variable has a null value.
The number expression test checks to see whether or not 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.
The odd expression test checks to see whether a numeric variable is an odd number.
Below is the same example as the inverse even expression test previously described.
The sameas expression test checks to see 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.
The sequence expression test is similar to the iterable test, in that it checks to see whether or not a variable is a sequence.
The example below checks whether a variable is a sequence and then iterates through that sequence of musical genres.
The string expression test checks to see whether the value stored in a variable is text.
The example below checks whether or not a variable is a string, and if so it applies a title filter to change the capitalization.
This expression test checks to see if a string starts with a particular string. It is used in conjunction with the "is" operator.
The truthy expression test checks to see whether an expression evaluates to True.
The example below uses a boolean checkbox module to display an alert message.
The undefined expression test checks to see whether a variable is undefined in the context of the template. This test is different from none, 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".
The upper expression test evaluates to true when a string is all uppercase. Below is an inverse example of the lower expression test above.
Thank you for your feedback, it means a lot to us.