基本介紹
- 外文名:StringTemplate
- 簡稱:ST
- 基於:Java
- 性質:模板引擎庫
特點,基本語法,
特點
使用StringTemplate可以嚴格保證業務邏輯和表現邏輯相分離,不會互相干擾,提高網站的開發和維護的效率。模型和視圖分離所帶來的優點包括:模板檔案可以在相似的網站開發中重用,清晰的模板檔案可以作為網站開發的說明文檔,模板檔案可以單獨修改。
基本語法
StringTemplate支持兩種分隔設定<...>和$...$
語法 | 描述 | 例子 |
<attribute> | 顯示變數值或空串 | $user$ <user> |
<attribute.property> | 顯示集合或對象的屬性值或空串 | $user.name$ <user.name> |
<attribute.(expr)> | 與<attribute.property> 意義相同,但是如果expr是保留字,則必須使用<attribute.(expr)> | $user.(name_label)$ <user.(name_label)> |
<multi-valued-attribute> | 集合中多個屬性連線在一起的值 | $users$ <users> |
<multi-valued-attribute; separator=expr> | 多個屬性連線在一起,屬性之間使用expr分隔 | $users; separator=", "$ <users; separator=", "> |
<template(argument-list)> | 調用其它模板,可以向該模板傳遞參數 | $bold()$ <bold(item=title)> |
<attribute:template(argument-list)> | 模板套用 | $name:bold()$ <name:checkoutReceipt(items=skus, ship=shipOpt)> |
<attribute:{argument-name_ | _anonymous-template}> | 套用匿名模板 | $users:{s|<li>$s$</li>}; separator="\n"$ <users:{s|<li>$s$</li>}; separator="\n"> |
<if(!attribute)>subtemplate<endif> | 條件語句 | $if(users)$ $users:{u|$u$}$ $endif$ <if(users)> <users:{u|<u>}> <endif> |
\$ or \< | 語法分隔設定的轉義表達 | \$ \< |
<\ >, <\n>, <\t>, <\r> | 轉義字元 | $\n$ <\n> |
<! comment !>, $! comment !$ | 注釋符號 | $! this is a comment !$ <! this is a comment !> |