【原创】推荐一个模板引擎 - templateengine
先看看他的語法吧
Thank?You?for?your?order?#order.billFirstName#?#order.billLastName#.
<br>
Your?Order?Total?is:?#format(order.total,?"C")#
<br>
<ad:if?test="#order.shipcountry?isnot?"US"#">
Your?order?will?arrive?in?2-3?weeks
<ad:else>
Your?order?will?arrive?in?5-7?days
</ad:if>
TemplateEngine2.0 主要功能
Template Engine主要是兩個類在使用:Template 和TemplateManager,Template 是一個基本模板, TemplateManager是擴展的模板
用Template或TemplateManager 是非常容易創建模板的
Template?template?=?Template.FromString(string?name,?string?data)
Template?template?=?Template.FromFile(string?name,?string?filename)
用TemplateManager
TemplateManager?mngr?=?new?TemplateManager(Template template);
或者
TemplateManager?mngr?=?TemplateManager.FromFile(string?filename);
TemplateManager?mngr?=?TemplateManager.FromString(string?template);
當調用FromString 方法時,string是模板的代碼。這個方法能動態的加載文本到template中。FormFile是指調用的模板文件。
TemplateManager有個屬性SetValue可以用來設置模板的變量和變量的內容
mngr.SetValue("xx",?"xxxxxxxxxxxxxx");
輸出模板
Response.Write(mngr.Process());
這一切都非常的簡單。
函數列表
equals(obj1.obj2) 比較兩個變量是否一樣,返回bool型
notequals(obj1,obj2)比較兩個變量是否不一樣,返回 bool型 也可以 not(equals(obj1,obj2))
iseven(num) 測試是否是偶數
isodd(num)測試是否奇數
isempty(string) 測試字符串是否為0
isnotempty(string) 測試字符串是否大于0
isnumber(num) 測試是否為整型
toupper(string) 轉化成大寫
tolower(string) 轉化成小寫
isdefined(varname) 測試變量是否有定義
ifdefined(varname, value) 返回 value 如果varname 是有定義的,
特別有用:#ifdefined("name", name)# 將輸出value 如果varname沒有定義,其它情況不輸出
len(string)返回字符串長度
tolist(collection, property, delim) 簡單的輸出列表
例1:
ArrayList?list?=?new?ArrayList();
list.Add("one");
list.Add("two");
list.Add("three");
template.SetValue("mylist",?list);
你的模板內容:
#toList(mylist, " & ")#
輸出:one & two & three
例2:
list.Add(new?Customer("Henry",?"III"));
list.Add(new?Customer("Tom",?"Jackson"));
template.SetValue("mylist",?list);
模板內容:
#toList(mylist, "firstName", ",")#
輸出:Tom,Henry,Tom
函數列表
snull(obj) 測試是否為空
not(boolvalue) 返回not(!)的bool型
iif(booleanExpression, iftruevalue, iffalsevalue) 如果booleanExpression為真,輸出iftruevalue,否則輸出iffalsevalue
format(object, formatstring)格式化字符串,相當于ToString(formatstring)
例子:
#format(total, "C")#
will output: $1,208.45
函數列表
trim(string) 去除前后空格
filter(collection, booleanproperty) 返回一個新的組所有符合booleanproperty要求的
gt(obj1,obj2) 如果 obj1 > obj2 返回為真,否則為false
lt(obj1,obj2) 如果 obj1 < obj2 返回為真,否則為false
compare(obj1, obj2) 如果 obj1<obj2 返回-1 obj1=obj2 返回0 如果obj1>obj2 返回1
or(bool1,bool2) 有一個為真返回就是true
and(bool1, bool2) 兩個都為真時返回true
comparenocase(string1, string2) 相等時返回為true 否則為false
stripnewlines(string) 返回 所有\r\n 用space 代替的字符串
typeof(object) 返回object 類型,如typeof("abcd234") 返回 "string" typeof(3) 返回 int
cint(value) 返回整數 等同于Convert.toInt32()
cdouble(value) 返回雙精度
cdate(value) 返回一個datetime 型
createtypereference(type) 引用一個靜態的類型,但格式必須是<ad:set tag
例:
#createtypereference("System.Math").Round(3.39789)#
#createtypereference("System.Math").PI#
or
<ad:set name="MyMath" value="#createtypereference("System.Math")#" />
#MyMath.Round(3.3)#
#MyMath.PI#
還支持c#內置的一些方法,如還可以這樣使用
#xx.Length#
還有很多你意想不到的效果,趕緊來試用吧。
轉載于:https://www.cnblogs.com/caicaihui/archive/2008/07/07/1237756.html
總結
以上是生活随笔為你收集整理的【原创】推荐一个模板引擎 - templateengine的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在C#中利用DirectX实现声音播放(
- 下一篇: Eclipse快捷键指南