May 14, 2021 Groovy
Groovy's template engine works like mail merge (automatically adding names and addresses from the database to letters and envelopes to make it easier to send messages, especially ads, to many addresses), but it's more generic.
If you take the following simple example, let's first define a name variable to hold the string "Groovy". In the println statement, we use the $symbol to define parameters or templates that can be inserted into values.
def name = "Groovy" println "This Tutorial is about ${name}"
If the above code is executed in groovy, the following output is displayed. The output clearly shows that the $name is replaced by the value assigned by the def statement.
The following is an example of SimpleTemplateEngine, which allows you to use JSP-like scriptlets and EL expressions in templates to generate paramethic text. b20> The template engine allows binding parameter lists and their values so that they can be replaced in strings with defined placeholders.
def text ='This Tutorial focuses on $TutorialName. In this tutorial you will learn about $Topic' def binding = ["TutorialName":"Groovy", "Topic":"Templates"] def engine = new groovy.text.SimpleTemplateEngine() def template = engine.createTemplate(text).make(binding) println template
If the above code is executed in groovy, the following output is displayed.
Now let's use the template feature for XML files. A s a first step, let's add the following code to a file called Student.template. me. o $name, $is, and $subject are all placed as placeholders that need to be replaced at runtime.
<Student> <name>${name}</name> <ID>${id}</ID> <subject>${subject}</subject> </Student>
Now let's add our Groovy script code to add functionality and replace the template above with the actual values. You should be aware of the following things about the following code.
The mapping of placeholders to actual values is done by binding and SimpleTemplateEngine. A binding is a map in which placeholders act as keys and replacement values as values.
import groovy.text.* import java.io.* def file = new File("D:/Student.template") def binding = ['name' : 'Joe', 'id' : 1, 'subject' : 'Physics'] def engine = new SimpleTemplateEngine() def template = engine.createTemplate(file) def writable = template.make(binding) println writable
If the above code is executed in groovy, the following output is displayed. As you can see from the output, the value was successfully replaced in the relevant placeholder.
<Student> <name>Joe</name> <ID>1</ID> <subject>Physics</subject> </Student>
The StreamingTemplateEngine engine is another template engine available in Groovy. 4k. n particular, this template engine can handle strings larger than 64k.
Here's an example of how to use StreamingTemplateEngine -
def text = '''This Tutorial is <% out.print TutorialName %> The Topic name is ${TopicName}''' def template = new groovy.text.StreamingTemplateEngine().createTemplate(text) def binding = [TutorialName : "Groovy", TopicName : "Templates",] String response = template.make(binding) println(response)
If the above code is executed in groovy, the following output is displayed.
This Tutorial is Groovy The Topic name is Templates
XmlTemplateEngine is used for template scenarios where both the template source and the expected output are XML. The template inserts any expression into the template using the normal $(expression) and $variable notation.
The following is an example of how to use XMLTemplateEngine.
def binding = [StudentName: 'Joe', id: 1, subject: 'Physics'] def engine = new groovy.text.XmlTemplateEngine() def text = ''' <document xmlns:gsp='http://groovy.codehaus.org/2005/gsp'> <Student> <name>${StudentName}</name> <ID>${id}</ID> <subject>${subject}</subject> </Student> </document> ''' def template = engine.createTemplate(text).make(binding) println template.toString()
If the above code is executed in groovy, the following output is displayed
<document> <Student> <name> Joe </name> <ID> 1 </ID> <subject> Physics </subject> </Student> </document>