简介
StringTemplate(简称ST)是一个基于Java的模板引擎库(已移植到C#、Python、Ruby和Scala),可以用于生成源代码、Web页面、电子邮件或任何其他格式的文本输出。ST不同于其他类似的模板引擎的显著特点是严格执行模型视图分离,尤其擅长多目标代码生成、多种网站风格管理以及网站多语言版本生成等。ST已经应用到许多大型网站中,例如jGuru.com。
ST在.net平台下的最新版本为3.2(版本号3.1.3.6002),这个版本开始使用Antlr3来实现,下载地址为:http://www.stringtemplate.org/download/StringTemplate-3.2-2009-06-28.7z,由于它使用了C#3.0语法,所以只能在.net framework 3.5以上版本运行,可以在http://fisheye2.atlassian.com/browse/antlrcs/Antlr3.StringTemplate找到它的源代码。
如果要在.net framework 1.1/2.0上使用ST,可以选择由Antlr2实现的3.1beta1或3.0.1版本,可以从http://stringtemplate.org/download.html下载(提供了全部源代码)。
ST的资料主要集中在官方网站http://www.stringtemplate.org/的Documentation、Wiki及Articles栏目,中文方面的资料非常欠缺,对于英文比较糟糕的我来说是一件痛苦的事情...
使用
下载StringTemplate-3.2-2009-06-28.7z,在项目里添加引用Antlr3.Runetime.dll、Antlr3.Runetime.Debug.dll、Antlr3.StringTempalte.dll。
using System; using Antlr3.ST; namespace Chapter01 { class Program { public static void Main(string[] args) { StringTemplate query = new StringTemplate("hi,$name$!"); query.SetAttribute("name", "囧月"); Console.WriteLine(query.ToString()); Console.ReadKey(true); } } }
输出结果:hi,囧月!
话说运行在Release下为什么要引用Antlr3.Runetime.Debug.dll,实在不解!
本文地址:http://www.cnblogs.com/lwme/archive/2010/04/29/1723810.html
参考:http://www.antlr.org/wiki/display/ST/StringTemplate+3.0+Printable+Documentation