Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

'eval' Method

eval(String script)

This method will evaluate a Groovy code from a string and return the result.

Code Block
languagetext
$groovy.eval("println 'Hello World!'")

eval(String script, String bindingName, Object bindingObject)

This method will evaluate a Groovy code with a single binding object and specified binding name. The code will be evaluated from a string. The binding name will be used as the name for this object.

Code Block
languagetext
#foreach ($c in $Class)
$groovy.eval("println classname", "classname", $c.name)
#end

eval(String script, Map bindingMap)

This method will evaluate a Groovy code with a set of binding arguments (a name and an object). The code will be evaluated from a string. The binding map consists of key-value pairs for the binding name and the binding object.

Code Block
languagetext
#set ($dict = $map.createHashMap())
#set ($void = $dict.put("first", "foo"))
#set ($void = $dict.put("last", "bar"))
$groovy.eval("println first + last", $dict)

Or

Code Block
languagetext
$groovy.eval("println first + ' ' + last", {"first":"foo", "last":"bar"})


Tip

The second code contains curly brackets; '{' and '}' characters, which are not allowed to be used in any RTF template. For the RTF template, use the first code instead.

'execute' method

execute(String filename)

This method will execute a Groovy file. The 'filename' parameter refers to a name of the Groovy file or an absolute path to the Groovy file.

Code Block
languagetext
$groovy.execute("filename.groovy")

If the Groovy file contains Groovy functions, you can recall the functions by using 'eval()' methods.

Code Block
languagetext
$groovy.execute('filename.groovy')
$groovy.eval("new groovy().functionname()")

execute(String filename, String bindingName, Object bindingObject)

This method will execute a Groovy file with a single binding object and specified binding name. The 'filename' parameter is a file path to the Groovy file. The binding name will be used as the name for this object.

File filename.groovy

Code Block
languagetext
"Class name is $c.name"

The template code

Code Block
languagetext
#foreach ($c in $Class)
$groovy.execute("filename.groovy", 'c', $c)
#end

execute(String filename, Map bindingMap)

This method will execute a Groovy file with a set of binding arguments (a name and an object). The 'filename' parameter is a file path to the Groovy file. The binding map consists of key-value pairs for the binding name and the binding object.

File filename.groovy

Code Block
languagetext
first + " " + last

The template code

Code Block
languagetext
#set ($dict = $map.createHashMap())
#set ($void = $dict.put("first", "foo"))
#set ($void = $dict.put("last", "bar"))
$groovy.execute("filename.groovy", $dict)


Tip
  • Absolute Path: If the 'filename' is provided with an absolute path, Groovy Tool will read the Groovy file from an absolute location such as $groovy.execute('c:/mycode/readclass.groovy').
  • Relative Path: If the 'filename' is provided with a relative path, Groovy Tool will read the template from a relative location. This relative location starts from the current directory in which the template is located such as $groovy.execute('readclass.groovy').