一、简单介绍
Beetl是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,它功能强大,性能良好,超过当前流行的模板引擎。而且还易学易用。
二、那么为什么要用模板引擎呢?
很简单的原因,实际应用场景中文本的格式是固定的,但是内容有所不同。如果是不复杂的内容我们可以直接用代码生成需要的文本。但是当文本变得复杂的时候,我们用java生成文本的性能就会下降,同时也不利于维护。解决办法是将数据和格式进行分离,将一个文本分成模板和数据。模板中有固定的格式,需要动态变化的数据一般用占位符代替。这样我们想改模板格式的时候不需要去更改代码,只需要去改模板就可以了。同时模板引擎渲染文本的效率也会更高。
三、如何上手
pom文件引入依赖
1 <dependency> 2 <groupId>com.ibeetl</groupId> 3 <artifactId>beetl-framework-starter</artifactId> 4 <version>1.1.55.RELEASE</version> 5 </dependency>
(1)直接在模板上绑定数据
1 public void beetlString() throws Exception{ 2 // 初始化模板资源加载器 3 StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader(); 4 // 配置Beetl,这里使用默认配置 5 Configuration config = Configuration.defaultConfiguration(); 6 // 初始化Beetl的核心GroupTemplate 7 GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, config); 8 // 自定义模板 9 String testTemplate = "<html>\n"+ 10 "\t<head>\n"+ 11 "\t\t<title>${title}</title>\n"+ 12 "\t</head>\n"+ 13 "\t<body>\n"+ 14 "\t\t<h1>${name}</h1>\n"+ 15 "\t<body>\n"+ 16 "</html>"; 17 // 通过GroupTemplate传入自定义模板加载出Beetl模板Template 18 Template template = groupTemplate.getTemplate(testTemplate); 19 // 使用Template中的操作,将数据与占位符绑定 20 template.binding("title","Beetl使用Demo"); 21 template.binding("name","Beetl使用练习1:直接绑定数据"); 22 // 渲染字符串 23 String str = template.render(); 24 System.out.println(str); 25 }
(2)使用Map绑定数据
1 public void beetlString() throws Exception{ 2 // 初始化模板资源加载器 3 StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader(); 4 // 配置Beetl,这里使用默认配置 5 Configuration config = Configuration.defaultConfiguration(); 6 // 初始化Beetl的核心GroupTemplate 7 GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, config); 8 // 自定义模板 9 String testTemplate = "<html>\n"+ 10 "\t<head>\n"+ 11 "\t\t<title>${title}</title>\n"+ 12 "\t</head>\n"+ 13 "\t<body>\n"+ 14 "\t\t<h1>${name}</h1>\n"+ 15 "\t<body>\n"+ 16 "</html>"; 17 // 通过GroupTemplate传入自定义模板加载出Beetl模板Template 18 Template template = groupTemplate.getTemplate(testTemplate); 19 // 使用Template中的操作,将数据与占位符绑定 20 Map<String, String> map = new HashMap<>(); 21 map.put("title","Beetl使用Demo"); 22 map.put("name","Beetl使用练习2:使用Map绑定数据"); 23 template.binding(map); 24 // 渲染字符串 25 String str = template.render(); 26 System.out.println(str); 27 }
四、在Beetl模板中使用循环语句
使用Map传入数据
1 public void beetlString() throws Exception{ 2 // 初始化模板资源加载器 3 StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader(); 4 // 配置Beetl,这里使用默认配置 5 Configuration config = Configuration.defaultConfiguration(); 6 // 初始化Beetl的核心GroupTemplate 7 GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, config); 8 // 自定义模板 9 String testTemplate = "<html>\n"+ 10 "\t<head>\n"+ 11 "\t\t<title>Beetl使用Demo</title>\n"+ 12 "\t</head>\n"+ 13 "\t<body>\n"+ 14 "\t\t<h1>Beetl使用练习</h1>\n"+ 15 "\t\t<%" + 16 " for(entry in map){" + 17 " print(entry.key+\":\"+entry.value);" + 18 " }"+ 19 "%>\n"+ 20 "\t<body>\n"+ 21 "</html>"; 22 // 通过GroupTemplate传入自定义模板加载出Beetl模板Template 23 Template template = groupTemplate.getTemplate(testTemplate); 24 // 传入Map格式数据,在Beetl模板中使用循环语句遍历Map 25 Map<String, String> map = new HashMap<>(); 26 map.put("one","123"); 27 map.put("two","321"); 28 template.binding("map",map); 29 // 渲染字符串 30 String str = template.render(); 31 System.out.println(str); 32 }
输出结果
<html>
<head>
<title>Beetl使用Demo</title>
</head>
<body>
<h1>Beetl使用练习</h1>
one:123two:321 <body>
</html>
五、条件语句
使用if语句
1 public void beetlString() throws Exception { 2 // 初始化模板资源加载器 3 StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader(); 4 // 配置Beetl,这里使用默认配置 5 Configuration config = Configuration.defaultConfiguration(); 6 // 初始化Beetl的核心GroupTemplate 7 GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, config); 8 // 自定义模板 9 String testTemplate = "<html>\n" + 10 "\t<head>\n" + 11 "\t\t<title>Beetl使用Demo</title>\n" + 12 "\t</head>\n" + 13 "\t<body>\n" + 14 "\t\t<h1>Beetl使用练习</h1>\n" + 15 "<%" + 16 "var a =true;" + 17 "var b = 1;" + 18 "if(a&&b==1){ print(\"a&&b==1\");" + 19 "}else if(a){ print(\"a=true\");" + 20 "}else{ print(\"else\");" + 21 "}" + 22 "%>" + 23 "\n\t<body>\n" + 24 "</html>"; 25 // 通过GroupTemplate传入自定义模板加载出Beetl模板Template 26 Template template = groupTemplate.getTemplate(testTemplate); 27 // 渲染字符串 28 String str = template.render(); 29 System.out.println(str); 30 }
switch语句也同样。
注意: switch变量可以支持任何类型,而不像js那样只能是整型。
1 <% 2 var b = 1; 3 switch(b){ 4 case 0: 5 print("it's 0"); 6 break; 7 case 1: 8 print("it's 1"); 9 break; 10 default: 11 print("error"); 12 } 13 %>
select-case语句是switch case的增强版。他允许case 里有逻辑表达式,同时,也不需要每个case都break一下,默认遇到合乎条件的case执行后就退出。
1 <% 2 var b = 1; 3 select(b){ 4 case 0,1: 5 print("it's small int"); 6 case 2,3: 7 print("it's big int"); 8 default: 9 print("error"); 10 } 11 %>
select 后也不需要一个变量,这样case 后的逻辑表达式将决定执行哪个case.其格式是
1 <% 2 var b = 1; 3 select{ 4 case b<1,b>10: 5 print("it's out of range"); 6 break; 7 case b==1: 8 print("it's 1"); 9 break; 10 default: 11 print("error"); 12 } 13 %>