1 using the template engine
The template stores only text. Templates can include reference variables and groovy code. Groovy's template engine provides a createTemplate
way to implement strings, Files, Readers, or URLs, returning an Template
object.
Template
Objects are typically used to create final text. Template
call the Make method, which returns a map of the Writable
key-value pairs passed in the Make method, which is the variable name and corresponding value of the passed-in template.
Package Template Import Groovy.text.SimpleTemplateEngine class templatetest { Static Main (args) { String Templatetext = "Project report: We have currently ${tasks.size} number of items with a total duration of $duration. <% tasks.each{%> -$it. Summary; <%}%> ‘‘‘; def list = [ New Task (Summary: "Learn Groovy", Duration:4), New Task (Summary: "Learn Grails", Duration:12) ]; def totalduration = 0; List. each {totalduration+=it.duration}; def engine = new simpletemplateengine (); def template = Engine.createtemplate (Templatetext); def binding = [ Duration: "$totalDuration", Tasks:list ]; println (Template.make (binding)). ToString (); } } |
Output
Project Report: We have currently 2 number of items with a total duration of 16. -Learn Groovy; -Learn Grails; |
18 using the template engine