httl 一個比freemarker 還牛逼的靜態框架
阿新 • • 發佈:2018-12-26
學習案例
-----------------------------------------轉型函式(可以與set配合,以供後調)-----------------------------------
obj.to("com.foo.Bar")
obj.toMap
num.toDate
str.toDate
str.toDate("yyyy-MM-dd HH:mm:ss")
str.toChar
str.toBoolean
str.toByte
str.toInt
str.toLong
str.toFloat
str.toDouble
str.toClass
str.toLocale
----------------------------------------------集合函式()-------------------------------------------------
陣列和List一樣可以用size方法獲取大小
array.size
list.size
map.size
list.sort
#for(item : list.sort)
#end
list.toCycle
#set(colors = ["red","blue","green"].toCycle)
#for(item : list)
${colors.next}
#end
--------------------------------------------日期操作符()---------------------------------------------------
date1 > date2
date1 >= date2
date1 < date2
date1 <= date2
--------------------------------------------集合操作符()---------------------------------------------------
${list[0]} 等價於:${list.get(0)}
${map.abc} 等價於:${map.get("abc")}
${map["a.b.c"]} 等價於:${map.get("a.b.c")}
序列生成: 1..3
比如:
#for(i : 1..10)
${i}
#end
List生成: [123,"abc", var]
比如:
#for(color : ["red","yellow","blue"])
${color}
#end
Map生成: ["xxx": 123,"yyy":"abc","zzz": var]
比如:(此Map保持宣告時的順序)
#for(entry : ["red":"#FF0000","yellow":"#00FF00"])
${entry.key} = ${entry.value}
#end
集合相加:list1 + list2
比如:
#for(item : list1 + list2)
${item}
#end
----------------------------------------------邏輯操作符()-------------------------------------------------
#if(object)
等價於:
#if(object != null)
#if(string)
等價於:
#if(string!= null && string.length() > 0)
#if(list)
等價於:
#if(list != null && list1.size() > 0)
#for(item : list1 || list2)
等價於:
#for(item : list1 != null && list1.size() > 0 ? list1 : list2)
例子如下:
demo_a.httl<!DOCTYPE html><html lang="en" ly:set="Book _Book, ItemVO item, KindVO vo"><head> <meta charset="UTF-8"> <title>模板樣例</title></head><body><br/> [email protected]@[email protected]@================================================================<br/>輸出:${_int}<br/>[email protected]@[email protected]@============================================================<br/>輸出:${_Integer}<br/>[email protected]@[email protected] @=============================================================<br/>輸出:${_String}<br/>[email protected]@[email protected]@=========================================================<br/>#set(BigDecimal price = _BigDecimal)輸出:${price.format("###,##.##")} [@此處有靜態方法處理價格@] <br/>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -實際呼叫下面的靜態方法:(被調物件作為第一個引數傳入,後面的引數原樣傳入,注意,型別要嚴格匹配)public static String format(Date date, String format) {return new SimpleDateFormat(format).format(date);}<br/> [email protected]@[email protected]@===============================================================<br/>輸出:${_Date}<br/>[email protected]@[email protected]@=============================================================<br/>輸出:</br><table> <tbody> <tr> <th>書名</th> <td>${_Book.title}</td> </tr> <tr> <th>作者</th> <td>${_Book.author}</td> </tr> <tr> <th>出版商</th> <td>${_Book.publisher}</td> </tr> <tr> <th>書名</th> <td>${_Book.title}</td> </tr> <tr> <th>出書日期</th> <td>${_Book.publication}</td> </tr> <tr> <th>讀者</th> <td> 直接讀取物件:${_Book.reader}<br/> <br/>--------------------------------<br/> <span>讀取列表</span> <br/>-----------------遍歷開始---------------<br/> <ul ly:for="String reader : _BList"> <li>${reader}</li> <li ly:if="for.last">共${for.size}位讀者------------------------此處是for的一些判斷位置:第${for.index+1}位</li> </ul> <br/>-----------------遍歷結束---------------<br/> <br/>-----------------二次接收物件遍歷開始 說明:<br/> 要先設定變數<br/> \#set(List readers = _Book.reader)<br/> ---------------<br/> #set(List readers = _Book.reader) <ul ly:for="String reader : readers"> <!-- 只輸出第一次--> <li ly:if="for.index==1">${reader}</li> </ul> <br/>-----------------二次接收物件遍歷結束---------------<br/> </td> </tr> </tbody></table><br/>=========================[email protected]@集合物件@@===========================================================<br/>輸出:<br/><table ly:for="Book r: _ListBook" style="border-bottom: 1px solid #aa0000"> <tbody ly:if="for.index<=2"> <tr> <th>書名</th> <td>${r.title}</td> </tr> <tr> <th>作者</th> <td>${r.author}</td> </tr> <tr> <th>出版商</th> <td>${r.publisher}</td> </tr> <tr> <th>書名</th> <td>${r.title}</td> </tr> <tr> <th>出書日期</th> <td>${r.publication}</td> </tr> </tbody></table><br/>================================================包含模板 開始========================================================<br/>#set(bk = _Book)$!{include("demo_b.httl")}<br/>================================================包含模板 結束========================================================<br/>=====================================================多層list集合複合物件 開始============================================<br/>${vo.name}手機<br/>#set(List subKindVO = vo.subKindVOList)<div ly:for="SubKindVO skvo:subKindVO"> #set(int one_for = for.index + 1) 第${one_for}層迴圈 第${for.index + 1}次 <span>${vo.name}:${skvo.name}</span><br/> #set(List labelVO = skvo.labelVOList) <div ly:for="LabelVO lvo:labelVO"> 第${one_for}層迴圈 ${skvo.name}的第${for.index + 1}次迴圈 <span>功能:${lvo.name}</span><br/> </div></div>=====================================================多層list集合複合物件 結束============================================<br/>==============================================驗證以下的傳參方式==========================================================<br/><!---->#[${include("/demo/demo_inc_c.httl",["download": "active"])}]# <br/>==================================================#[if else 判斷語法]#======================================================<br/><a ly:if="1==1" href="http://www.baidu.com/">百度</a><a ly:else="2==1" href="#">谷歌</a>========================================================================================================<br/>========================================================================================================<br/>========================================================================================================<br/></body></html>demo_b.httl
<!DOCTYPE html>
<html lang="en" ly:set="Book _Book, Book bk">
<head>
<meta charset="UTF-8">
<title>模板樣例</title>
</head>
<body>
接收父模板資料:========================================================================================================<br/>
@@@${bk.title}@@
int:${_int}<br/>
${_BigDecimal}被格式化後結果是: <br/>
#set(BigDecimal price = _BigDecimal)
BigDecimal:${price.format("###,##.##")}<br/>
Date:${_Date}<br/>
Book:
<table>
<tbody>
<tr>
<th>書名</th><td>${_Book.title}</td>
</tr>
<tr>
<th>作者</th><td>${_Book.author}</td>
</tr>
<tr>
<th>出版商</th><td>${_Book.publisher}</td>
</tr>
<tr>
<th>書名</th><td>${_Book.title}</td>
</tr>
<tr>
<th>出書日期</th><td>${_Book.publication}</td>
</tr>
<tr>
<th>讀者</th>
<td>
直接讀取物件:${_Book.reader}<br/>
<br/>--------------------------------<br/>
<span>讀取列表</span>
<br/>-----------------遍歷開始---------------<br/>
<ul ly:for="String reader : _BList">
<li>${reader}</li>
</ul>
<br/>-----------------遍歷結束---------------<br/>
<br/>-----------------二次接收物件遍歷開始 說明:<br/>
要先設定變數<br/>
\#set(List readers = _Book.reader)<br/>
---------------<br/>
#set(List readers = _Book.reader)
<ul ly:for="String reader : readers">
<li>${reader}</li>
</ul>
<br/>-----------------二次接收物件遍歷結束---------------<br/>
</td>
</tr>
</tbody>
</table>
<br/>
</body>
</html>
demo_inc_c.httl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
include 傳參成功
結果:>${download}<
include 傳參成功 end
</body>
</html>