[CS2] Template Operators
Operators are symbols that allow you to compare two values or combine two values.
Comparison Operators
Many times you whether in using a ternary condition or in conditional rendering you'll want to use a comparison operator. This page documents what is available to you in the component studio templating language. These operators will return 1 if true or 0 if false.
Operator | Description | Example |
== | Equal to. Note that = is not the same thing. | {{quantity == 5}} {{color == 'blue'}} |
!= | Not equal to. | {{quantity != 5} } {{color != 'blue'}} |
> | Greater than. | {{quantity > 2}} |
< | Less than. | {{quantity < 2}} |
>= | Greater than or equal to. | {{quantity >= 2}} |
<= | Less than or equal to. | {{quantity <= 2}} |
if else | Ternary condition. It takes 3 arguments. The first is the true value condition to validate, and then the false value. | {{true if level > 2 else false}} |
Arithmetic Operators
Perform a mathematical calculation upon two values.
Operator | Description | Example | Result |
+ | Addition | {{1 + 1}} | 2 |
- | Subtraction | {{1 - 1}} | 0 |
* | Multiplication | {{1 * 1}} | 1 |
/ | Division | {{1 / 1}} {{5 / 2}} |
1 2.5 |
// | Division with truncation. | {{1 // 1}} {{5 // 2}} |
1 2 |
% | Modulus, the remainder after division. | {{1 % 1}} {{5 % 3}} |
1 2 |
** | The power of another number. | {{2**2}} | 4 |
Logical Operators
Perform logic operations on values to determine a true or false value.
Operator | Description | Example | Result |
not | Not. Invalidates the value. | {{not 1}} {{not 3 < 1}} |
false true |
and | And. | {{0 and 0}} {{1 and 0}} {{1 and 1}} {{ 3 > 1 and 1 > 0}} |
false false true true |
or | Or. | {{0 or 0}} {{1 or 0}} {{1 or 1}} {{ 3 > 1 or 1 > 0}} |
false true true |
if else |
Ternary. It takes 3 arguments. The first is the true value then the condition to validate, and then the false value. Note that the false value is optional. | {{ 'purple' if position == 'king' else 'white'}} {{ 'purple' if position == 'king'}} |
purple purple |
String Operators
Use the to manipulate strings.
Operator | Description | Example | Result |
+ | Concatenate. Combines two strings together. | {{'R' + 'G' + 'B'}} {{first_name + ' ' + last_name}} |
RGB Andy Dufresne |
Null
When you are trying to check for a field that is undefined or empty, then you'll want to use the special keyword 'null'. For example, if you have a field that might be empty and you don't want to render it when empty, you could add this to conditional rendering to do this:
{{this_might_be_empty == null}}