StringTemplate.NET实例7

时间:2009-11-07 阅读: 佚名
类还是实例6的类,直接写代码: User us1 = new User(); us1.Name = "张三"; us1.Age = "23"; User us2 = new User(); us2.Name = "李四"; us2.Age = "24"; User us3

类还是实例6的类,直接写代码:

            User us1 = new User();
            us1.Name = "张三";
            us1.Age = "23";
            User us2 = new User();
            us2.Name = "李四";
            us2.Age = "24";
            User us3 = new User();
            us3.Name = "王五";
            us3.Age = "25";
            User us4 = new User();
            us4.Name = "赵六";
            us4.Age = "26";
            List<User> uss = new List<User>();
            uss.Add(us1);
            uss.Add(us2);
            uss.Add(us3);
            uss.Add(us4);
            StringTemplateGroup sg = new StringTemplateGroup("GroupTest");
            sg.DefineTemplate("RowRed",
              "<tr class=red><td>$it.Name$</td><td>$it.Age$</td></tr>\r\n");
            sg.DefineTemplate("Rowblack",
              "<tr class=black><td>$it.Name$</td><td>$it.Age$</td></tr>\r\n");
            StringTemplate st = sg.DefineTemplate("List",
              "<table>\r\n$Item:RowRed(),Rowblack()$</table>");
            st.SetAttribute("Item", uss);

-----------------------------------------------------------------

结果如下:

<table>
<tr class=red><td>张三</td><td>23</td></tr>
<tr class=black><td>李四</td><td>24</td></tr>
<tr class=red><td>王五</td><td>25</td></tr>
<tr class=black><td>赵六</td><td>26</td></tr>
</table>

[责任编辑:jumbot]

[发表评论] [收藏本页]