Template parameters

You can pass parameters of any type to your self-contained Gosu template files. The syntax for defining parameters for a template is:

<%@ params(ARGLIST) %>

ARGLIST is an argument list such as for a standard Gosu function.

You can use template parameters in template files, but not in String literals that use template syntax. In a template file, you can use a parameter in either the template expression syntax (${ and }) or template scriptlet syntax (<% and %>). The expression syntax always returns a result and generates additional text. The scriptlet syntax executes Gosu statements.

For example, suppose you create a template file NotifyAdminTemplate.gst within the package mycompany.templates. Edit the file to contain the following lines of code:

<%@ params(personName : String, contactHR: boolean) %>
The person ${ personName } must update their contact information in the company directory.

<% if (contactHR) { %>
Call the human resources department immediately.
<% } %>

In this example, the if statement, including its trailing brace, is within scriptlet tags. The if statement uses the parameter value at run time to conditionally run elements that appear later in the template. This template exports the warning to call the human resources department only if the contactHR parameter is true. Use if statements and other control statements to control the export of static text in the template as well as template expressions.

To run your template, you use the following code:

var x : String = mycompany.templates.NotifyAdminTemplate.renderToString("hello", true)

If you want to export to a character writer, use code like the following:

var x : String = mycompany.templates.NotifyAdminTemplate.render(myWriter, "hello", true)
Important: Gosu has no supported API to generate template output from within a template scriptlet. Instead, design your template to combine template scriptlets and template expressions using the code pattern in this topic.