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);
        //}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值