An identifier by itself refers to a scalar in the root hash of the data model:
${foo}
The delimiters mean "print the variable's value here". An expression that has no referent, or whose referent is null, produces no output. Expressions inside template tags don't have these delimiters:
<if foo> hello </if>
This prints "hello" if the boolean value of foo is true. In templates, undefined, null, and empty values evaluate to false; all other values evaluate to true. Undefined, null, and empty values are considered to be equal.
If two identifiers are separated by a dot, the one on the left is a hash; the one on the right is the name of a key in that hash:
<p>First Name: ${book.author.firstName}
When an identifier followed by an expression in brackets, the expression is evaluated at run time as the name of a hash key. The following are all equivalent, if propName equals "bar" and propStart equals "ba":
foo.bar foo["bar"] foo[propName] foo["ba" + "r"] foo[propStart + "r"]
You can chain together dynamic hash keys. The following are equivalent:
foo.bar.baz.bing foo["bar"]["baz"]["bing"]
However, the brackets cannot be followed by a dot or an identifier.
In addition to the dot and bracket operators, the following operators are supported in expressions:
Operator | Usage | Meaning |
---|---|---|
! | !exp | Logical not |
+ | exp + exp | String concatenation |
== | exp == exp | Equality (string comparison) |
!= | exp != exp | Inquality (string comparison) |
&& | exp && exp | Logical and |
|| | exp || exp | Logical or |
String literals are delimited by double quotes. A backslash in a string literal can be used to escape a double quote. To include a backslash character, use \\. Operators have standard precedence, and you can use nested parentheses.
An identifier followed a pair of parentheses is a method call:
foo() foo("hello") deal(king, queen, spade)
A method can return a list, a hash, a scalar, or null. You can chain together method calls if each one returns a hash containing the next method:
foo().bar()
Here, foo() returns a hash in which the key bar's value is a method object that executes bar().
Here are some more examples of syntactically valid expressions:
!(color == "blue" || color == "violet") "customer" + customer.number foo && (!(bar || baz) || bing) eventManager.getEvent(eventID).importance
If you use a boolean expression for its string value (not something you'll often need to do), its string value is "true" if it evaluates to true, otherwise null.
Previous: Introduction | Next: Lists |