这里有一个简单例子:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Templates API:在模版引擎中有两个主要的类:
Template和TemplateManager.
Template含有一个简单的模版文件实例,TemplateManager被用来处理模版.
创建模版对象Template实例的方法是通过Template或者TemplateManager的静态方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
或者可以这么做:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
可以使用SetValue(string name, object value);来处理模版文件中的标签.
Ex:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
- Expressions 表达式(标签,应该这么理解把)
在模版文件中标签是以#包起来的字符串:
#FirstName#
这个例子会输出first name的值.如果要输出#,只要连写两个#即可.
Ex:
Your SS## is #ssnumber#
使用标签你可以输出任何变量(对象):
#somevar#
输出对象的一个属性或字段:
#somestring.Length#
属性名称不区分大小写,所以你可以:#string.length# or #string.LENGTH#
或者调用一个函数:#trim(somename)#
#customer.firstname.length#
你可以调用任何对象的方法:
#firstname.substring(0, 5)# 或者 #customer.isValid()#
Ver2.1允许你调用指定下标的Array项:
#somearray[3]# - 得到Array的第三项
#hastable["somekey"]# - 得到somekey的值从hashtable.
有一些现成的函数和很方便就能添加的函数,这些函数是:
equals(obj1, obj2) - 检查obj1和obj2是否相等,返回boolean真假
notequals(obj1, obj2) - 返回!equals(obj1, obj2). 相当于调用: not(equals(obj1, obj2))
iseven(num) - tests whether number is an even number
isodd(num) - tests whether number is an odd number
isempty(string) - 测试字符串是否为空. 和equals(string.Length, 0)相同
isnotempty(string) - 测试字符串是否含有至少1个字符
isnumber(num) - 测试num是否为数字格式
toupper(string) - 不说了
tolower(string) - 不说了
isdefined(varname) - 测试varname是否被定义
ifdefined(varname, value) - 如果varname被定义,那么返回value的值.例如:#ifdefined("name", name)# -如果name被定义的话,返回name的值,否则返回nothing.
len(string) - 返回字符串的长度.
tolist(collection, property, delim) -以delim为分隔符,把collection转化为字符串.prototype是collection的某个属性,如果prototype不为空,将返回以delim分隔的该属性的值.如果没有prototype,collection将被使用.
Ex:
假设list如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后的输出将是:one & two & three
假设list如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后的输出将是:Tom,Henry,Tom
isnull(obj) - 测试对象是否为null
not(boolvalue) - -_-不说了
iif(booleanExpression, iftruevalue, iffalsevalue) - 和c#中的三元表达式一样: booleanExpression ? iftruevalue : iffalsevalue
Ex:
#iif(isodd(i), "bgcolor=yellow", "bgcolor=red")#
will output bgcolor=yellow if i is odd number and bgcolor=red if i is not odd number
format(object, formatstring) - 将会调用object的ToString(formatstring).
Ex:
(如果total是货币型1208.45)
#format(total, "C")#
会输出: $1,208.45
trim(string) - 略
filter(collection, booleanproperty) - will return new List from collection for those objects whose booleanproperty property evaluates to true
gt(obj1, obj2) - 如果obj1>obj2返回true.obj1和obj2必须继承于相同的类型.
lt(obj1, obj2) - 如果obj1<obj2返回true.obj1和obj2必须继承于相同的类型.
compare(obj1, obj2) - 如果obj1<obj2,返回-1;如果obj1==obj2,返回0,如果obj1>obj2返回1.obj1和obj2必须继承于相同的类型.
or(bool1, bool2) - 如果bool1为真或bool2为真,返回真
ex:
#or(equals(state, "IL"), equals(state, "NY"))# - 如果state=="IL"或者state=="NY"返回真
and(bool1, bool2) - 不罗嗦了
comparenocase(string1, string2) - will do case insenstive comparison of string1 and string2 and return true if they are equal
stripnewlines(string) - 在string后换行
typeof(object) - Ex: typeof("hello") return "string". typeof(3) returns int
cint(value) - 调用Convert.ToInt32 from .net library
cdouble(value) - 转换为double类型
cdate(value) - 转换为datetime类型.Ex: #cdate("2005-5-1")#
createtypereference(type) - Ex:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(未完,等续)
AderTemplate官方网站: http://www.adersoftware.com
AderTemplate dll下载地址: http://www.aspxon.com/ShowSoft/15.aspx
AderTemplate源码下载地址(含有演示): AderTemplate(dll)模版引擎(c#源码)