beetl for循環渲染html字符串的方式,
beetl if條件判斷輸出,
beet自定義標簽和標簽引用,
beetl html賦值,
beetl渲染json,beetl注釋、變量定義,
更多文檔請到:http://ibeetl.com/guide/#beetl
beetl for循環輸出
beetl for輸出select option <select> @for(item in list){ <option value="${item.id}">${item}</option> @} </select> beetl for輸出ul li html <ul> @for(item in list){ <li>${item}</li> @} </ul>
beetl定界符和占位符
- @ 和回車換行
- #: 和回車換行
- <!--: 和 -->
- <!--# 和 -->
- <? 和 ?>
- 占位符--#{ }-##
定界符號里是表達式,如果表達式跟定界符或者占位符有沖突,可以在用 “\” 符號,如
@for(user in users){ email is ${user.name}\@163.com @} ${[1,2,3]} //輸出一個json列表 ${ {key:1,value:2 \} } //輸出一個json map,} 需要加上\
beetl注釋
Beetl語法類似js語法,所以注釋上也同js一樣: 單行注釋采用// 多行注視采用/* */ <% /*此處是一個定義變量*/ var a = 3; //定義一個變量. /* 以下內容都將被注釋 %> <% */ %>
beetl賦值
賦值於html中常用 ${ 服務端的變量 } 來做,這個於jsp是一致的。例如: <input value="${value}"> <div>${html}</div>
beetl if選擇性輸出變量格式化
支持三元表達式 ${a==1?"ok":''} ${a==1?"ok"} <input type="checkbox" ${a==0? "checked"}> <input ${a==0?"readonly"} /> <select> <option ${a==1?"selected"}>hello</option> <option>world</option> </select>
beetl標簽函數
如果共用一個模版html 方法一: <% layout("/temlet/layout.html"){ %> //這里可以寫上html <% } %> 方法二: @layout("/temlet/layout.html"){ //這里可以寫上html @}
允許 include 另外一個模板文件
<% include("/inc/header.html"){} %>
自定義HTML標簽
<#button name="提交" handle="add()"/> file: /* button.tag */ <button onclick="${handle}">${name}</button>