`
wyf
  • 浏览: 424852 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
社区版块
存档分类
最新评论

.StringTemplate替换模板

 
阅读更多

官方下载 www.StringTemplate.org .Net 组件,在项目中引用antlr.runtime.dll和StringTemplate.dll

引用 using Antlr.StringTemplate;

多个值的语法如下:$value;null="xxx",separator=", "$ 定义value属性,当value为null则显示xxx,如果有多个属性值则以“,”号进行分隔(其中null和separator分别定义属性为空时的默认值和分隔符)。

 

//简单变量替换
        //StringTemplate hello = new StringTemplate("Hello, $name$");
        //hello.SetAttribute("name", "World");

        //简单变量替换
        //StringTemplate hello = new StringTemplate("SELECT $column; separator=\",\"$ FROM $table$;");
        //hello.SetAttribute("column", "name");
        //hello.SetAttribute("column", "email");
        //hello.SetAttribute("column", "sex");
        //hello.SetAttribute("table", "User");

        //复杂变量替换
        //StringTemplate hello = new StringTemplate("复杂变量替换 $Text;null=\"为空值\",separator=\",\"$ 真是复杂啊!");
        //hello.SetAttribute("Text", "中国", null, "台湾", "印度");

        //键值类型替换
        //StringTemplate hello = new StringTemplate("对象变量替换 姓名:$KeyList.Name$, 年龄:$KeyList.Age$ ");
        //Hashtable ht = new Hashtable();
        //ht.Add("Name", "李四");
        //ht.Add("Age", "35");
        //hello.SetAttribute("KeyList", ht);

        //同时显示多个变量
        //StringTemplate hello = new StringTemplate("变量相加 $[Temp1,Temp2,Temp3]$");
        //StringTemplate hello = new StringTemplate("变量相加 $[Temp1,Temp2,Temp3];separator=\",\"$");
        //hello.SetAttribute("Temp1", "变量1");
        //hello.SetAttribute("Temp2", "变量2");
        //hello.SetAttribute("Temp3", "变量3");

        //模板调用
        //StringTemplateGroup sg = new StringTemplateGroup("GroupTest");
        //sg.DefineTemplate("Box", "中华人民共和国中华人民共和国,中华人民共和国");
        //StringTemplate hello = sg.DefineTemplate("List", "下面是我调用Box模板内容:\n$Box()$");

        //给调用模板传参数
        //StringTemplateGroup sg = new StringTemplateGroup("GroupTest");
        //sg.DefineTemplate("BoxA", "我不来了()");
        //sg.DefineTemplate("Box", "中华人民共和国,中华全国 Title: $Title$ !!");
        //StringTemplate hello = sg.DefineTemplate("List", "下面是我调用Box模板内容:\n$Box(Title=\"标题值\")$"); //传变量值
        //StringTemplate hello = sg.DefineTemplate("List", "下面是我调用Box模板内容:\n$Box(Title={$Titles$})$"); //传变量
        //StringTemplate hello = sg.DefineTemplate("List", "下面是我调用Box模板内容:\n$Box(Title=BoxA())$"); //传模板
        //hello.SetAttribute("Titles", "标题参数值");

        //值模板
        StringTemplateGroup sg = new StringTemplateGroup("GroupTest");
        sg.DefineTemplate("Box", "中华人民共和国,,,,");
        StringTemplate hello = new StringTemplate(sg, "调用值模板:$Mys:Box();separator=\"==\"$------s");
        hello.SetAttribute("Mys", "中国");
        hello.SetAttribute("Mys", "中国");

        //循环显示
        //StringTemplate hello = new StringTemplate("<table>$Item:{<tr><td>$it$要循环显示的内容</td></tr>}$</table>");
        //for (int i = 0; i < 10; i++)
        //{
        //    hello.SetAttribute("Item", i);
        //}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics