18 using the template engine

Source: Internet
Author: User
Tags grails

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.

TemplateObjects are typically used to create final text. Templatecall 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.