zoukankan      html  css  js  c++  java
  • 18 使用模板引擎

    1       使用模板引擎

    模板存放的只是文本。模板能包括引用变量和groovy代码。groovy的模板引擎提供了createTemplate 方法来实现Strings, Files, Readers or URL,返回的是Template 对象。

    Template 对象通常用来创建最终的文本。Template 调用make方法,返回的是Writable,而make方法中传入的是键值对的map,而该map是传入模板的变量名及对应的值。

    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();

        }

     

    }

    输出

    Project report:

              

              

               We have currently 2 number of items with a total duration of 16.

              

                  - Learn Groovy;

              

                  - Learn Grails;

              

          

  • 相关阅读:
    CH4302 Interval GCD
    20201017 模拟赛总结
    LOJ#2610. 「NOIP2013」货车运输
    POJ3696 The Luckiest number
    20200925 小练习总结
    更新一波状态
    AFO
    【扩展lucas定理】
    【LCT维护子树信息】uoj207 共价大爷游长沙
    [bzoj4589]Hard Nim(FWT快速沃尔什变化+快速幂)
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/5719203.html
Copyright © 2011-2022 走看看