stringtemplate模板引擎使用Demo,
如下:假如美工提供給我們的靜態頁面是如下的效果.
以下為程序代碼:
<!--Setting Strict DOCTYPE--> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!--Setting Your/s NameSpace,All Page is Same--> <html xmlns="http://www.w3.org/1999/xhtml" lang="big5"> <HTML> <HEAD> <TITLE> New Document </TITLE> <!--Setting Text Encoding--> <meta http-equiv="Content-Type" content="text/html; charset=big5" /> <!--New Version Browser--> <meta http-equiv="Content-Language" content="big5" /> <!--Old Version Browser--> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <!--UI Code--> <table border="1"> <tr> <td>No.</td> <td>姓名</td> <td>工號</td> </tr> <tr οnmοuseοver="javascript:this.style.backgroundColor=''Aqua'';"οnmοuseοut="javascript:this.style.backgroundColor=''white''"> <td>1</td> <td>jimmy</td> <td>fXXXXXX</td> </tr> <tr οnmοuseοver="javascript:this.style.backgroundColor=''Aqua'';"οnmοuseοut="javascript:this.style.backgroundColor=''white''"> <td>2</td> <td>aa</td> <td>fasdsb</td> </tr> <tr οnmοuseοver="javascript:this.style.backgroundColor=''Aqua'';"οnmοuseοut="javascript:this.style.backgroundColor=''white''"> <td>3</td> <td>bb</td> <td>sdfsdfsd</td> </tr> </BODY> </HTML> |
我們把這段代碼存為template.html,
當我們拿到這個靜態頁面,以前我們首先要做的就是將頁面中的table用net中的服務器控件,datalist,Gridview等數據控件替換,但是現在卻不用,我們要做的僅僅是將顯示數據的地方替換成stringtemplate所要求的格式,再將該html另存為st格式(暫不知道是否支持其它格式的模板定義文件),如下:
以下為程序代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="big5"> <head> <title> New Document </title> <!--Setting Text Encoding--> <meta http-equiv="Content-Type" content="text/html; charset=big5" /> <!--New Version Browser--> <meta http-equiv="Content-Language" content="big5" /> <!--Old Version Browser--> </head> <body> <!--UI Code--> <table border="1"> <tr> <td>No.</td> <td>姓名</td> <td>工號</td> </tr> $userInfo:{ <tr οnmοuseοver="javascript:this.style.backgroundColor=''Aqua'';"οnmοuseοut="javascript:this.style.backgroundColor=''white''"> <td>$it.id$</td> <td>$it.name$</td> <td>$it.applicant$</td> </tr>}$ </table> </body> </html> |
存為template.st.
當然,還有最后一步就是讀取數據進行處理.
.cs
以下為程序代碼:
使用前提:下載stringtemplate的dll,并加入參考, 引入命名空間: using Antlr.StringTemplate.Language; using Antlr.StringTemplate; |