StringTemplate cheat sheet

Expression elements

See Expressions

Syntax

Description

<attribute>

Evaluates to the value of attribute.ToString() if it exists else empty string.

<i>, <i0>

The iteration number indexed from one and from zero, respectively, when referenced within a template being applied to an attribute or attributes.

<attribute.property>

Looks for property of attribute as a property (C#), then accessor methods like getProperty() or isProperty(). If that fails, StringTemplate looks for a raw field of the attribute called property. Evaluates to the empty string if no such property is found.

<attribute.(expr)>

Indirect property lookup. Same as attribute.property except use the value of expr as the property_ name. Evaluates to the empty string if no such property is found.

<multi-valued-attribute>

Concatenation of ToString() invoked on each element. If multi-valued-attribute is missing his evaluates to the empty string.

<multi-valued-attribute; separator=expr>

Concatenation of ToString() invoked on each element separated by expr.

<[mine, yours]>

Creates a new multi-valued attribute (a list) with elements of mine first then all of yours.

<template(argument-list)>

Include template. The argument-list is a list of attribute assignments where each assignment is of the form arg-of-template=expr where expr is evaluated in the context of the surrounding template
not of the invoked template.

<(expr)(argument-list)>

Include template whose name is computed via expr. The argument-list is a list of attribute assignments where each assignment is of the form attribute=expr. Example $(whichFormat)()$ looks up whichFormat's value and uses that as template name. Can also apply an indirect template to an attribute.

<attribute:template(argument-list)>

Apply template to attribute. The optional argument-list is evaluated before application so that you can set attributes referenced within template. The default attribute it is set to the value of attribute. If attribute is multi-valued, then it is set to each element in turn and template is invoked n times where n is the number of values in attribute. Example: $name:bold() applies bold() to name's value.

<attribute:(expr)(argument-list)>

Apply a template, whose name is computed from expr, to each value of attribute. Example $data:(name)()$ looks up name's value and uses that as template name to apply to data.

<attribute:t1(argument-list): ... :tN(argument-list)>

Apply multiple templates in order from left to right. The result of a template application upon a multi-valued attribute is another multi-valued attribute. The overall expression evaluates to the concatenation of all elements of the final multi-valued attribute resulting from templateN's application.

<attribute:{anonymous-template}>

Apply an anonymous template to each element of attribute. The iterated it atribute is set automatically.

<attribute:{argument-name_ | _anonymous-template}>

Apply an anonymous template to each element of attribute. Set the argument-name to the iterated value and also set it.

<a1,a2,...,aN:{argument-list_ | _anonymous-template}>

Parallel list iteration. March through the values of the attributes a1..aN, setting the values to the arguments in argument-list in the same order. Apply the anonymous template. There is no defined it value unless inherited from an enclosing scope.

<attribute:t1(),t2(),...,tN()>

Apply an alternating list of templates to the elements of attribute. The template names may include argument lists.

<first(attr)>

The first or only element of attr. You can combine operations to say things like first(rest(names)) to get second element.

<last(attr)>

The last or only element of attr.

<rest(attr)>

All but the first element of attr. Returns nothing if $attr$ a single valued.

<trunc(attr)>

returns all but last element

<strip(attr)>

Returns an iterator that skips any null values in $attr$. strip(x)
=x when x is a single-valued attribute.

<length(attr)>

Return an integer indicating how many elements in length $attr$ is. Single valued attributes return 1. Strings are not special; i.e., length("foo") is 1 meaning "1 attribute". Nulls are counted in lists so a list of 300 nulls is length 300. If you don't want to count nulls, use length(strip(list)).

\$ or \<

escaped delimiter prevents $ or < from starting an attribute expression and results in that single character.

<\ >, <\n>, <\t>, <\r>

special character(s): space, newline, tab, carriage return. Can have multiple in single <...> expression.

<\uXXXX>

Unicode character(s). Can have multiple in single <...> expression.

<! comment !>, $! comment !$

Comments, ignored by StringTemplate.

Statements

See Conditionally included subtemplates

Syntax

Description

<if(attribute)>subtemplate
<else>subtemplate2
<endif>

If attribute has a value or is a boolean object that evaluates to true, include subtemplate else include subtemplate2. These conditionals may be nested.

<if(x)>subtemplate
<elseif(y)>subtemplate2
<elseif(z)>subtemplate3
<else>subtemplate4
<endif>

First attribute that has a value or is a boolean object that evaluates to true, include that subtemplate. These conditionals may be nested.

<if(!attribute)>subtemplate<endif>

If attribute has no value or is a bool object that evaluates to false, include subtemplate. These conditionals may be nested.

Groups

See Group Files

group name
t1(args) ::= "template1"
t2(args) ::= <<
template2>>

Also

group name implements interfacename;
...

where the interface is defined via:

interface interfacename;
t1(args);
t2(args);

Reserved words

Don't use these as attribute names or template names:

default
first
group
if

implements
interface
last
length

optional
rest
strip
super

trunc
else
endif
elseif