C#中Nvelocity的简单使用

把Nvelocity封装好的dll文件添加引用到你的项目中。

然后导入命名空间

using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;

下面来写一个简单的例子

/*
*加载引擎
*/
VelocityEngine vlte
= new VelocityEngine();
vlte.SetProperty(RuntimeConstants.RESOURCE_LOADER,
"file");
vlte.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,Server.MapPath(
"~/"));
vlte.Init();
/*
*填充内容
*/
VelocityContext vlc
= new VelocityContext();
vlc.Put(
"Name", "李雷");
vlc.Put(
"Age","24");
/*
*指定引擎加载的模版
*/
Template vtp
= vlte.GetTemplate("Model.htm");
StringWriter str
=new StringWriter();
vtp.Merge(vlc, str);
Response.Write(str.GetStringBuilder().ToString());
好了,直接在模版中调用后跑一下程序就ok了~!

posted @ 2011-04-18 11:04  奶嘴儿  阅读(1139)  评论(0)    收藏  举报