`
wyf
  • 浏览: 433632 次
  • 性别: 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);
        //}

分享到:
评论

相关推荐

    前端开源库-stringtemplate-js

    1. **模板(Template)**: 模板是包含占位符的字符串,这些占位符将会被实际数据替换。例如,`"Hello, ${name}"`就是一个简单的模板,其中`${name}`是一个占位符。 2. **属性(Attribute)**: 属性是模板中的占位符...

    String Template API.chm

    StringTemplate的核心概念是模板,它是一种包含静态文本和可替换占位符的特殊字符串。这些占位符可以是变量、表达式或者嵌套的模板。通过这种方式,模板定义了输出的结构,而实际的数据则在运行时被插入到模板的相应...

    StringTemplate遇见jQuery冲突的解决方法

    它在生成模板时,会按照定义的模板结构替换其中的变量和表达式,以适应不同的输出需求。 jQuery是一个快速、小巧的JavaScript库。它通过一个易于使用的API使得HTML文档遍历和操作、事件处理、动画和Ajax等操作变得...

    StringTemplate:用于PHP的简单死亡字符串模板引擎

    StringTemplate StringTemplate是一个非常简单的php字符串模板引擎。 我已经编写了类似sprintf的东西,但是有命名和嵌套替换。 有关安装说明,请转到本自述文件的末尾。为什么我经常要与sprintf缺少命名占位符功能作...

    C# 实现生成Word报表

    此外,如果你的项目需要大量生成Word文档,或者对性能有较高要求,可以考虑使用模板引擎,如Freemarker或StringTemplate。这些模板引擎能更好地分离数据和格式,使代码更清晰,同时提高生成效率。 总结起来,利用C#...

    template:递归模板引擎

    模板 递归模板引擎。 字符串模板 最基本的模板是字符串模板。 它将包含{{ variables }}的字符串替换为作为替换传递的值:...数组模板像StringTemplate一样呈现其值。 $ replacements = array ( 'vehicle' =&gt; 'car' ,

    动软代码生成器源码

    1. **模板引擎**:动软代码生成器可能采用了某种模板引擎,如NVelocity或StringTemplate,这些模板引擎允许开发者定义代码模板,模板中的变量和控制结构会被实际数据替换,生成最终代码。 2. **数据库元数据解析**...

    C# 代码生成器源码 最新版

    常见的模板引擎有NVelocity、Spark View Engine和StringTemplate等。这些引擎允许开发者定义模板文件,其中包含可替换的占位符,然后用实际数据填充这些占位符。 2. **元数据解析**:元数据通常用于描述程序的结构...

    javascript文本模板用法实例

    在介绍的JavaScript文本模板用法实例中,使用了一个自定义的字符串模板引擎类`StringTemplate`来模拟文本模板的功能。这个类可以将带有特定占位符的字符串转换为根据给定映射动态填充内容的最终字符串。 这个实例中...

    word动态生成

    7. **模板引擎**: 对于复杂的文档结构,可以使用模板引擎(如StringTemplate或FreeMarker),先创建一个Word模板,然后用编程语言替换占位符,生成最终文档。 8. **性能优化**: 使用Interop创建大量文档时,可能会...

    DDBuilder数据库文档生成工具V1.5.2发布

    在工作中需要生成文档总需要生成文档...2、调整了模板解析引擎,使用velocity替换了stringtemplate。 3、完善了表结构导出,特别是对表和字段的说明导出 4、增加了MySQL的文档导出支持 DDBuilder操作起来更方便。

    set-drafter:可以从数据文件夹中创建包含文本项的文本文件的项目起草人

    5. 模板引擎:如果项目集需要遵循特定的格式,可能需要实现一个简单的模板引擎,比如使用字符串替换或者更复杂的模板库如`StringTemplate`或`NVelocity`。 6. 多线程或异步处理:对于大量文件,为了提高性能,可能...

Global site tag (gtag.js) - Google Analytics