Overview of templates

Gosu provides a syntax that supports in-line dynamic templates. Use a template to combine static text with values from variables or other calculations Gosu evaluates template values at run time. For example, suppose you want to display text with a calculation in the middle of the text:

var s = "One plus one equals ${ 1 + 1 }."

If you print this variable, Gosu displays:

One plus one equals 2.

Template expressions can include variables and dynamic calculations. Gosu substitutes the run-time values of the expressions in the template. The following line is an example of a method call inside a template:

var s2 = "The total is ${ myVariable.calculateMyTotal() }."

At compile time, Gosu ensures all template expression are valid and type safe. At run time, Gosu runs the template expression, which must return a String value or a type that can cast to a String.

In addition to in-line Gosu templates, Gosu supports a powerful file-based approach for Gosu templates with optional parameter passing. Any use of the parameters is validated for type-safety, just like any other Gosu code. For example, use a template to generate a customized notification email, and design the template to take parameters. Parameters could include type safe references to the recipient email address, the sender email address, and other objects. Insert the parameters directly into template output, or call methods or get properties from parameters to generate your customized email report.

See also