Create and use a template file

About this task

This example shows a common design pattern for templates that need to combine complex logic in scriptlet syntax with generated text into the template within a loop. This example produces static text. A typical template uses one or more parameters to produce dynamic output.

Procedure

  1. Using Studio, create a new template. In the Project pane within the gsrc section, right-click a package. Next, click New > Gosu Template. In the New Gosu Template dialog, type a meaningful name, and then click OK.

    For example, the fully qualified name of your template could be mycompany.templates.IntegerStringsInArray.

  2. Use a template scriptlet to create a static array or other iterable object:
    1. Use <% to begin your definition and type normal Gosu code to define the object.
      <% var MyArray : ArrayList<String> = {
        "1",
        "Two",
        "3.0"
      }
    2. End the scriptlet, using %>.

      Alternatively, define a parameter that specifies an array or other iterable object.

  3. Optionally, generate some static text.
    For example:
    Testing strings for number content:
  4. Use a template scriptlet to iterate over your object. For example:
    1. Use <% to begin your loop. For example:
      <% for (var obj in MyArray) {
    2. Before ending the scriptlet, set up a variable with your data to export and optionally perform application logic.
      var string = obj.toString()
      try {
        obj.toDouble()
    3. End the scriptlet, using %>.
  5. Insert a template expression to export the value of your variable, by enclosing a Gosu expression with ${ and } tags. Use your variable and, optionally, static text to produce data to export. Use more scriptlet tags as necessary to produce valid Gosu code.
    ${ string } is a number<%} catch (e : Exception) { %>
    "${ string }" is not a number
  6. Optionally, generate some static text.
  7. Add another template scriptlet by using <% and %> to contain code that closes your loop. Remember that scriptlets share scope across all scriptlets in that file, so you can reference other variables or close loops or other Gosu structural elements.
      <% }
    }%>
  8. If Studio is running a server, you must load the changed file by remaking the project. From the menu, click Build > Make Project.
  9. In Gosu Scratchpad, run the template by using code like the following:
    print(mycompany.templates.IntegerStringsInArray.renderToString())

Results

Running this code produces text like the following:

1 is a number
"Two" is not a number
3.0 is a number

Example

The complete code for this template file example looks like:

<% var MyArray : ArrayList<String> = {
  "1",
  "Two",
  "3.0"
}
%>
Testing strings for number content:
<% for (var obj in MyArray) {
  var string = obj.toString()
  try{
    obj.toDouble() %>
    ${ string } is a number<%} catch (e : Exception) { %>
    "${ string }" is not a number
  <% }
}%>

What to do next

See also