cTemplate Expressions
A number of cTemplate Statements, including #ASSIGN, #CALC and #IF, take an expression parameter. An expression can be a simple value or variable, but it can also include text, math, logic and functions to produce the ultimate value that will be passed as the parameter value. The following are supported in cTemplate expressions.
Constants
Keyword | Meaning |
---|---|
true | Boolean TRUE |
false | Boolean FALSE |
null | NULL |
undefined | Not set or invalid result (divide by zero) |
Text
Text is represented by a single or double-quoted string of characters.
#ECHO("This is a string of double-quoted text.")
#ECHO('This is a string of single-quoted text.')
Escape Sequences
Certain characters, such as the surrounding quote character, new lines, tabs are preceded by a backslash.
#ECHO("This is a string with \"quoted text\" and a trailing new line.\n")
UTF8 character codes are also preceded by a backslash to specify any UTF8 character.
#ECHO("This is a utf8 character: \u263a")
Appending Strings
Multiple strings and variables can be appended together with the plus sign (+).
#ECHO("Hello" + ' World.')
Variables
#ASSIGN($a, ${b.Field})
#CSCRIPT
$a = ${b.Field};
$a = $b.Field; // {} notation not required in #CSCRIPT
#ENDCSCRIPT
Arrays and Collections
#/ initialize arrays
#ASSIGN($array1, [])
#ASSIGN($array2, [1,2,3])
#/ output array2's 3rd element: 3
${array2[2]}
#CSCRIPT
$array1 = [];
$array2 = [1,2,3];
$array2 []= 4;
#ENDCSCRIPT
Associative Arrays
#ASSIGN($array1, [])
#ASSIGN($array2, ['a': 1, 'b': 2, 'c': 3])
${array2['b']}
#CSCRIPT
$array2 = [];
$array3 = ['a': 1, 'b': 2, 'c': 3];
$array3['a'] = 1;
$array3['b'] = 2;
$array3['c'] = 3;
#ENDCSCRIPT
Objects
#ASSIGN($object, {})
#{$object.Property = 'value';}
${object.Property}
#{
$object = {}; // initial properties can not be set yet
$object.Property = 'value';
}
Assignment
Operator | Description |
---|---|
= | Equal |
+= | Add (numbers) or append (string) |
-= | Subtract (numbers) |
*= | Multiply (numbers) |
/= | Divide (numbers) |
Comparison Operators
Operator | Description |
---|---|
== or eq | Equal |
!= or ne | Not Equal |
=== | Exactly Equal (same type and value) |
!== | Not Exactly Equal (same type and value) |
<=> | Sort comparison: -1, 0 or 1 |
< or lt | Less than |
> or gt | Greater than |
<= or le | Less than or Equal |
>= or ge | Greater than or Equal |
Logic
Operator | Description |
---|---|
|| | Logical OR |
&& | Logical AND |
Math
Operator | Description |
---|---|
+, -, *, /, % | Add, Subtract, Multiply, Divide, Modulo (divide and return remainder) |
++ | Increment |
-- | Decrement |
=~ | Regular expression match or substitution, formatted like Perl, except uses PHP's preg_match() and preg_replace() internally. |
Ternary Operator (?:)
Provides a shorthand #IF/#ELSE.
#ASSIGN($y, $x == 5 ? 5 : 0)
Undefined Coalescing Operator (??)
Provides a shorthand #IF($x !== UNDEFINED) $x #ELSE default #ENDIF. This is the same as the Null Coalescing Operator in PHP.
#ASSIGN($y, $x ?? 'default')
Directives and Functions
All cTemplate Directives and cScript Functions are allowed, as if already in #CSCRIPT. In other words, #COUNT($list)
can appear in an expression as well as count($list)
.
Example
#ASSIGN($array, [1, 2, 3])
#IF(#COUNT($array) < 3)
There are less than 3 items.
#ELSEIF(count($array) >= 3)
There are 3 or more items.
#ENDIF