Expression elements
Syntax |
Description |
|---|---|
<attribute> |
Evaluates to the string value of attribute (toString(), ToString(), _str_()) 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. Is only visible to the template being applied to the iterator values. |
<attribute.property> |
Looks for property of attribute as a property (C#), then accessor methods like |
<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 string values of the elements. If multi-valued-attribute is missing, it evaluates to the empty string. |
<multi-valued-attribute; separator=expr> |
Concatenation element string values separated by expr. |
<[mine, yours]> |
Creates a new multi-valued attribute (a list) with elements of |
<template(argument-list)> |
Include template. The argument-list is a list of attribute expressions or attribute assignments where each assignment is of the form arg-of-template=expr. expr is evaluated in the context of the surrounding template not of the invoked template. Example, |
<(expr)(argument-list)> |
Include template whose name is computed via expr. The argument-list is a list of attribute expressions or attribute assignments where each assignment is of the form attribute=expr. Example |
<attribute:template(argument-list)> |
Apply template to attribute with optional argument-list. Example: |
<attribute:(expr)(argument-list)> |
Apply a template, whose name is computed from expr, to each value of attribute. Example |
<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:{x | anonymous-template}> |
Apply an anonymous template to each element of attribute. The iterated value is set to argument x. The anonymous template references <x> to access the iterator value. |
<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. |
<attribute:t1(),t2(),...,tN()> |
Apply an alternating list of templates to the elements of attribute. The template names may include argument lists. |
|
escaped delimiter prevents |
|
special character(s): space, newline, tab, carriage return. Can have multiple in single <...> expression. |
|
Unicode character(s). Can have multiple in single <...> expression. |
|
Ignore the immediately following newline char. Allows you to put a newline in the template to better format it without actually inserting a newline into the output |
|
Comments, ignored by StringTemplate. |
Functions
Syntax |
Description |
|---|---|
<first(attr)> |
The first or only element of attr. You can combine operations to say things like first(rest(names)) to get second element. |
<length(attr)> |
Return the length of a mult-valued attribute or 1 if it is single attribute. If attribute is null return 0. 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)). |
<strlen(attr)> |
Return the length of a string attribute; runtime error if not string. |
<last(attr)> |
The last or only element of attr. |
<rest(attr)> |
All but the first element of attr. Returns nothing if <attr> is single valued. |
<reverse(attr)> |
Return a list with the same elements as v but in reverse order. null values are NOT stripped out. use reverse(strip(v)) to do that. |
<trunc(attr)> |
returns all elements but last element |
<strip(attr)> |
Return a new list w/o null values. |
<trim(attr)> |
Trim whitespace from back/front of a string; runtime error if not string. |
Statements
Syntax |
Description |
|---|---|
<if(attribute)>subtemplate |
If attribute has a value or is a boolean object that evaluates to |
<if(x)>subtemplate |
First attribute that has a value or is a boolean object that evaluates to |
<if(!attribute)>subtemplate<endif> |
If attribute has no value or is a |
Conditional expressions can include "or" and "and" operations as well as parentheses. E.g.,
Groups
To import other templates, use the import statement:
The paths can be absolute, but should probably be relative to the class path or the directory of the template that imports them.
Reserved words
Don't use these as attribute names or template names:
true, false, import, default, key, group, implements, first, last, rest, trunc, strip, trim, length, strlen, reverse, if, else, elseif, endif, delimiters.
1 Comment
Hide/Show CommentsSep 06, 2011
Burt Harris
It would probably be good to specify if the reserved words are case sensitive. Is there an escape mechanism if the model logically dictates one of the reserved words be used as an attribute name?