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 !$
|
註釋
|
|