httl 一个比freemarker 还牛逼的静态框架

来源:互联网 发布:javascript启示录 云盘 编辑:程序博客网 时间:2024/06/06 00:19

学习案例


-----------------------------------------转型函数(可以与set配合,以供后调)-----------------------------------obj.to("com.foo.Bar")obj.toMapnum.toDatestr.toDatestr.toDate("yyyy-MM-dd HH:mm:ss")str.toCharstr.toBooleanstr.toBytestr.toIntstr.toLongstr.toFloatstr.toDoublestr.toClassstr.toLocale----------------------------------------------集合函数()-------------------------------------------------数组和List一样可以用size方法获取大小array.sizelist.sizemap.sizelist.sort#for(item : list.sort)#endlist.toCycle#set(colors = ["red","blue","green"].toCycle)#for(item : list)${colors.next}#end--------------------------------------------日期操作符()---------------------------------------------------date1 > date2date1 >= date2date1 < date2date1 <= 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}#endList生成: [123,"abc", var]比如:#for(color : ["red","yellow","blue"])${color}#endMap生成: ["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/>===============================================@@int@@================================================================<br/>输出:${_int}<br/>===============================================@@Integer@@============================================================<br/>输出:${_Integer}<br/>===============================================@@String@@=============================================================<br/>输出:${_String}<br/>===============================================@@BigDecimal@@=========================================================<br/>#set(BigDecimal price = _BigDecimal)输出:${price.format("###,##.##")}  [@此处有静态方法处理价格@] <br/>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -实际调用下面的静态方法:(被调对象作为第一个参数传入,后面的参数原样传入,注意,类型要严格匹配)public static String format(Date date, String format) {return new SimpleDateFormat(format).format(date);}<br/>===============================================@@Date@@===============================================================<br/>输出:${_Date}<br/>===============================================@@ItemVO@@=============================================================<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/>===============================================@@集合对象@@===========================================================<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>

0 0