Template expressions

Expressions are used in conjunction with template keywords. <%if expects its condition to be boolean, so -boolValue is called on the resulting object. <%for expects its collection expression to evaluate to an object that supports Objective-C fast enumeration. <%print outputs a string or the object's -description if the expression does not evaluate to a string.

<h1><%print people.@count> people</h1>
<%for person in people>
    My name is <%print person.name.HTML> and I'm <%print person.age> years old.<br/>
<%end>

This document explains the syntax and behavior of expressions in TL, the system upon on which WATemplate is built.

Valid expression examples

[[NSMutableArray alloc] init]
6 * (2 * myNumberVar)
myObject.dictionaryProperty.key == 36
name != "John Doe" && name != "Jane Doe"
[object setDoubleValue:[anArray count]+1];
arrayObj.@avg.age > 32

There is currently only one prefix operator; !, negation. Negation is performed by calling -boolValue on the object and returning a number with the negated value. This works just like in C; 0 turns into 1, and everything else turns into 0. A value can be followed by any number of pairs of periods and keys. These are fed to KVC's valueForKeyPath:. This is similar to the dot syntax for properties in Objective-C, but can be used more generally, like for accessing values in dictionaries. Be careful when dealing with classes that use special behavior for valueForKey:, such as NSArray and NSSet.

Method invocations

TL uses classic Objective-C syntax for calling methods. Everything in the TL environment is an object, but you can pass NSNumber objects as arguments to methods that expect primitive numbers. Similarly, returned primitives are wrapped in NSNumbers. Other values, like structs, are converted to and from NSValue objects when possible.

[myArray objectAtIndex:[myArray count]-2]

String literals

String literals evaluate to NSStrings and work mostly like in Objective-C. The leading @ is optional, and some rarely used escape sequences have been left out.

@"This is a string"
"but so is this \u263A"

Number literals

4  298563  3.95  -34  .81

Identifiers

Identifiers are simple names like foo, hello123 or _something that refer to variables, constants or classes. WATemplate allows you to set variables using -setValue:forKey: in code, and with the <%set keyword in templates. There are a few predefined constants: nil, NULL, YES, NO.

<%set myVar = myDict.someKey>
<%print myVar>

Infix operators

P Symbol Function Description
1 * Multiplication Evaluates to the result of calling -TL_multiply: on the left operand. Currently implemented for NSNumber.
/ Division Evaluates to the result of calling -TL_divide: on the left operand. Currently implemented for NSNumber.
2 + Addition Evaluates to the result of calling -TL_add: on the left operand. Currently implemented for NSNumber.
- Subtraction Evaluates to the result of calling -TL_subtract: on the left operand. Currently implemented for NSNumber.
3 < Less than Evaluates to true if compare: returns NSOrderedAscending
<= Less than or equal Evaluates to true if compare: returns NSOrderedAscending or NSOrderedSame
4 > Greater than Evaluates to true if compare: returns NSOrderedDescending
>= Greater than or equal Evaluates to true if compare: returns NSOrderedDescending or NSOrderedSame
5 === Identity equality Evaluates to true if the left operand is the same object as the right operand (pointer comparison).
!== Identity inequality Evaluates to true if the left operand is not the same object as the right operand (pointer comparison).
6 == Value equality Evaluates to the result of isEqual:
!= Value inequality Evaluates to the result of isEqual:, negated
7 && Logical AND Evaluates to true if boolValue returns YES for both operands.
8 || Logical OR Evaluates to true if boolValue returns YES for either operand.

Boolean values are returned as NSNumbers.