The following in Groovy 2.4.4:
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import groovy.text.TemplateEngine;
import java.util.Map;
...
// assume templateFile is a File object pointing to a GSP file
TemplateEngine templateEngine = new SimpleTemplateEngine(getClass().getClassLoader());
Template template = templateEngine.createTemplate(templateFile);
Map<String, Object> bindings = new Map<String, Object>();
String output = template.make().toString();
given a template file containing:
<%
var someVar = false;
%>
produces the error:
An unexpected error occurred while processing the template: No signature of method: SimpleTemplateScript369.var() is applicable for argument types: (java.lang.Boolean) values: [false] Possible solutions: wait(), run(), run(), every(), any(), wait(long)
How should a Boolean be created in a GSP template in this version of Groovy?
That error indicates that var doesn't accept a boolean value.
var
is used in some languages for variable definition, so I was mixing it up in my head withdef
which is the more relaxed variable type in Groovy.If you want to stop getting confused when switching between languages, in GSP just use the Java type when you are defining variables, e.g.: