今天用了一下 NVelocity, 感觉还不错, 虽然最后的release 版 是2003年的, 但和vs 2008 以及范型等兼容性没有问题。
Write a Simple sample:
Template:
Cool, this is a Velocity Template
let's get start:
## this is comments
#set($user="jimmy")
hello $user!!!!!
$fruits
the gameName is: $game1.GameName
#foreach ($fruit in $fruits)
!!! I Like $fruit
#end
.NET Code:
public class Game
{
public string GameName{get;set;}
public string GameType{get;set;}
public int Year{get;set;}
}
private void btnRender_Click(object sender, EventArgs e)
{
//tbTemplate.Text;
Velocity.Init("nvelocity.properties");
VelocityContext context = new VelocityContext();
Game game1 = new Game { GameName = "DOOM", GameType = "FST", Year = 1990 };
context.Put("name", "Velocity");
context.Put("project", "Jakarta");
context.Put("game1", game1);
List<String> strList = new List<String>();
strList.Add("Apple");
strList.Add("Orange");
strList.Add("Banana");
context.Put("fruits", strList);
//Template template = new Template();
StringWriter writer = new StringWriter();
Velocity.Evaluate(context, writer, "string1", tbTemplate.Text);
tbResult.Text = writer.ToString();
}Output:
Cool, this is a Velocity Template
let's get start:
hello jimmy!!!!!
System.Collections.Generic.List`1[System.String]
the gameName is: DOOM
!!! I Like Apple
!!! I Like Orange
!!! I Like Banana
若从源码编译: 几点需要注意
1) 它所用的log4net 版本比较老, 可能有兼容性问题
2) directive.properties 和 nvelocity.properties 两个文件需要设成 Embedded Resource
Links:
http://nvelocity.sourceforge.net/
http://velocity.apache.org