public class TemplateEngine extends java.lang.Object
The template engine is similar to Ruby ERB or Java Server Pages. Golo code and directives can be embedded as follows:
<% code %>
blocks contain any Golo code, and
<%= expression %>
outputs the evaluation of expression
, and
<%@params foo, bar, baz %>
makes the template function take these parameter names, and
<%@import foo.bar.Baz %>
is equivalent to a import
in a Golo module.
Here is a template example:
<%@params persons %>
<% foreach (person in persons) { %>
Name: <%= person: name() %>
Email: <%= person: email() orIfNull "n/a" %>
<% } %>
The resulting function would take a single parameter persons
. When no @params
clause is being
specified, template functions are assumed to take a single params
parameter.
It is important to note that this template engine performs no validation, either on the template itself or the
generated function code. One may however catch the GoloCompilationException
that compile(String)
may throw, and inspect the faulty code using
GoloCompilationException.getSourceCode()
and
GoloCompilationException.getProblems()
.
Constructor and Description |
---|
TemplateEngine() |
Modifier and Type | Method and Description |
---|---|
FunctionReference |
compile(java.lang.String template)
Compile a template into a function.
|
java.lang.String |
templateToGolo(java.lang.String template)
Generates the Golo code for a given template, but does not compile it.
|
public TemplateEngine()
public FunctionReference compile(java.lang.String template)
@params clause
, or
a single params
argument if none exists.template
- the template code.String
.GoloCompilationException
- if a compilation error occurs in the generated Golo code.public java.lang.String templateToGolo(java.lang.String template)
template
- the template code.