stringTemplate是一个模板引擎,同时支持java,C#,Python. 大家可以从http://www.stringtemplate.org下载. StringTemplate 语法说明 StringTemplate的语法是以$xxxx$来进行分割的. stringtemplate关键写是区分大小写的. 属性引用 名称属性 在模板中,这个是最常用的一个属性.用于显示文字.如下:
你的邮件地址:$email$
替换属性名为email的值.
同一个属性可以有多个值,多个值的语法如下 $value;null="xxx",separator=", "$ 定义value属性,当value为null则显示xxx.如果有多个属性值则以,号分开
字段引用 如果一个属性名称是对象或集合.可以用 属性名称.字段名 访问字段值 例如: 你的姓名: $人.姓名$ 你的邮件:$人.邮件$
使用语法: $对象名.字段名$ 在C#可以直接将一个对象设置到一个属性名称中. 如: User us = new User(); us.Name = "xxsssx"; us.Value ="80"; StringTemplate st = new StringTemplate("$User.Name$,$User.Value$"); st.SetAttribute("User", us); Console.WriteLine(st.ToString()); 对于键/值对象,也同样使用上面方式进行访问如: StringTemplate a = new StringTemplate("$user.name$, $user.phone$"); Hashtable user = new Hashtable(); user.Add("name", "Terence"); user.Add("phone", "none-of-your-business"); a.SetAttribute("user", user); string results = a.ToString(); 自定义属性字段名 格式: $属性名:{it.字段名}$ 例如: StringTemplate st = new StringTemplate("$abcdef:{第一个: $it.ddddd$ 第二个:$it.ddddd$}$"); st.SetAttribute("abcdef.{ddddd,ddddd}","中国人", "我不来了"); Console.WriteLine(st.ToString()); 如果字段名是保留字,可以使用$对象名.("保留字")$ 一次显示多个属性 $[属性名,属性名]$ 模板引用 必需把模板加入同一个模板组,才能相互之间调用模板. 通过$模板名()$来调用模板 模板传参数 $模板名(参数名=参数值,参数名=参数值)$ 例如: StringTemplateGroup Group = new StringTemplateGroup("Temp"); Group.DefineTemplate("link", "<a href='$url$'>$title$</a>"); StringTemplate st = new StringTemplate(Group, "调用link模板,显示链接 $link(url=/"/faq/view?ID=/"+faqid, title=faqtitle)$ ,真的啊!"); st.SetAttribute("faqid", 1); st.SetAttribute("title","中华人民共和国"); Console.WriteLine(st.ToString()); 循环显示使用 User us = new User(); us.Name = "哈哈"; us.Value = "99"; List<User> uss = new List<User>(); uss.Add(us); uss.Add(us); uss.Add(us); uss.Add(us); StringTemplate st = new StringTemplate("<table>$User:{<tr>$it.Name$<td></td>$it.Value$</tr>}$</table>"); st.SetAttribute("User", uss); Console.WriteLine(st.ToString()); 通过模板交替显示 StringTemplateGroup group = new StringTemplateGroup("Test"); group.DefineTemplate("TrRed", "<tr class=red><td>$it.name$</td><td>$it.value$</td></tr>/n"); group.DefineTemplate("TrWither", "<tr class=wither><td>$it.name$</td><td>$it.value$</td></tr>/n"); StringTemplate st = new StringTemplate(group, "<table>$User:TrRed(),TrWither()$</table>"); User us = new User(); us.Name = "哈哈哈"; us.Value = "999"; List<User> uss = new List<User>(); uss.Add(us); uss.Add(us); uss.Add(us); st.SetAttribute("User", uss); Console.WriteLine(st.ToString());
例子教程下载:下载
附:英文语法介绍
Syntax | Description | <attribute> | 获得定义attribute的.ToString值 | <i>, <i0> | 循环列表的索引值,<i>是从1开始计算,<i0>是从0开始计算 | <attribute.property> | 获取定义Attribute的字段值.如:User us = new User(); 将us设置为attribute属性,就可以通过User.字段名 来进行获取值 | <attribute.(expr)> | 和上面效果一样,都是取值.区别在于,如果你的字段名是保留字的话,请用attribute.("字段名")来进行访问. | <multi-valued-attribute> | 显示集合类型的 ToString() | <multi-valued-attribute; separator=expr> | 以分separator指定的分割符号,显示集合类型的ToString() | <template(argument-list)> | 调用模板,并传参数. 语法:模板名(参数) | <(expr)(argument-list)> | 和上面一样功能,如果模板名为保留字,可以使用("模板名")(参数)进行调用 | <attribute:template(argument-list)> | Apply template to attribute. The optional argument-list is evaluated before application so that you can set attributes referenced within template. The default attribute it is set to the value of attribute. Ifattribute is multi-valued, then it is set to each element in turn and template is invoked n times where n is the number of values in attribute. Example: $name:bold() applies bold() toname's value. | <attribute:(expr)(argument-list)> | Apply a template, whose name is computed fromexpr, to each value of attribute. Example$data:(name)()$ looks up name's value and uses that as template name to apply to data. | <attribute:t1(argument-list): ... :tN(argument-list)> | Apply multiple templates in order from left to right. The result of a template application upon a multi-valued attribute is another multi-valued attribute. The overall expression evaluates to the concatenation of all elements of the final multi-valued attribute resulting from templateN'sapplication. | <attribute:{anonymous-template}> | Apply an anonymous template to each element ofattribute. The iterated it atribute is set automatically. | <attribute:{argument-name_ | _anonymous-template}> | Apply an anonymous template to each element ofattribute. Set the argument-name to the iterated value and also set it. | <a1,a2,...,aN:{argument-list_ | _anonymous-template}> | Parallel list iteration. March through the values of the attributes a1..aN, setting the values to the arguments in argument-list in the same order. Apply the anonymous template. There is no defined itvalue unless inherited from an enclosing scope. | <attribute:t1(),t2(),...,tN()> | 根据attribute值的数,循环调用模板 | <if(attribute)>subtemplate <else>subtemplate2 <endif> | 条件判断,如果attribute为空或不是一个bool类型返回false | <if(!attribute)>subtemplate<endif> | 条件判断 如果attribute 为空或不是一个bool类型值,返回 true | <first(attr)> | 返回第一个属性值,如果你想返回第二个值可以使用first(rest(names)) | <last(attr)> | 返回最后一个属性值 | <rest(attr)> | 返回除去第一个属性值的所有属性值. | <strip(attr)> | 去除属性里包含的空值 | <length(attr)> | 返回属性包含的属性值个数,如果你不想统计空值可使用length(strip(list)). | /$ or /< | 语法分隔符 | </ >, </n>, </t>, </r> | 转译字符 | <! comment !>, $! comment !$ | 注释 | |