一、簡單介紹
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 %>