【原创】推荐一个模板引擎 - templateengine

一直都在使用StringTemplate模板引擎,虽然使用方便,但是功能实在太弱太弱,准备放弃使用StringTemplate。刚好碰巧发现了另外一个开源的模板引擎,就是今天要介绍的TemplateEngine 2,功能非常强大。

先看看他的语法吧

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("Tom""Whatever"));
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#

还有很多你意想不到的效果,赶紧来试用吧。

官方地址:http://www.adersoftware.com/index.cfm?page=templateEngine2
posted @ 2008-07-07 21:31  菜菜灰  阅读(10133)  评论(47编辑  收藏  举报