NVelocity模板引擎初学总结。

前不久,接触到.NET下的MVC-MonoRail,它推荐使用的模板引擎就是NVelocity(目前由Castle Project项目组接手)
因此决定自学一下NVelocity的使用(抛开MonoRail)。
--
首先:在Castle Project上下载一个CastleProject包,我下载的是CastleProject-1.0-RC3.msi
安装后,在其下的bin目录中可找到NVelocity.dll(NET项目中将用到),并将其复制出来放到我的测试WEB/BIN目录下。
到castleproject上看了一下using it大致有四步:
先要引入以下名称空间:
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例
VelocityEngine velocity = new VelocityEngine(); //也可以使用带参构造函数直接实例。
ExtendedProperties props = new ExtendedProperties();
velocity.Init(props);

第二步:Creating the Template加载模板文件
这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
Template template = velocity.GetTemplate( @"path/to/myfirsttemplate.vm");

第三步:Merging the template整合模板
VelocityContext context = new VelocityContext();
context.Put( "from", "somewhere");
context.Put( "to", "someone");
context.Put( "subject", "Welcome to NVelocity");
context.Put( "customer", new Customer( "John Doe") );

第四步:创建一个IO流来输出模板内容。推荐使用StringWriter(因为template中以string形式存放)
StringWriter writer = new StringWriter();
template.Merge(context, writer);
Response.Write(writer.ToString());

---通过上述步骤就可以轻松的使用NVelocity模板引擎的技术了。
有没有发现最后的Response.Write(writer.ToString())?
这个是直接输入到页面上。如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
生成静态页--是的,这是让大家都心动的。 
下面的代码是我第一个练习:
None.gif using  Commons.Collections;
None.gif
using  NVelocity;
None.gif
using  NVelocity.App;
None.gif
using  NVelocity.Context;
None.gif
using  NVelocity.Runtime;
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 这个测试是基于NVelocity模板引擎实现的.
ExpandedBlockEnd.gif
/// </summary>

None.gif public   partial   class  NVelocity_模板引擎测试 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//创建NVelocity引擎的实例对象
InBlock.gif
        VelocityEngine velocity = new VelocityEngine();
InBlock.gif        
//初始化该实例对象
InBlock.gif
        ExtendedProperties props = new ExtendedProperties();
InBlock.gif        props.AddProperty(RuntimeConstants.RESOURCE_LOADER, 
"file");
          //可换成:props.AddProperty("resouce.loader","file"),以下的同道理
InBlock.gif        props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
InBlock.gif        props.AddProperty(RuntimeConstants.INPUT_ENCODING, 
"gb2312");
InBlock.gif        props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, 
"gb2312");
InBlock.gif        velocity.Init(props); 
InBlock.gif        
//从文件中读取模板
InBlock.gif
        Template temp = velocity.GetTemplate("myTemplate.html");
InBlock.gif        IContext context 
= new VelocityContext();
InBlock.gif        context.Put(
"from""Sichuan");
InBlock.gif        context.Put(
"to""hainan");
InBlock.gif        context.Put(
"subject""welcome to nvelocity");
InBlock.gif        context.Put(
"name""McJeremy");
InBlock.gif        
//合并模板
InBlock.gif
        StringWriter writer = new StringWriter();
InBlock.gif        
//velocity.MergeTemplate(context, writer);
InBlock.gif
        temp.Merge(context, writer);
InBlock.gif        
//输入
InBlock.gif
        Response.Write(writer.ToString().Replace("\r\n""<br/>"));
ExpandedSubBlockEnd.gif    }
    
ExpandedBlockEnd.gif}
以下是生成静态页的练习:
None.gif using  Commons.Collections;
None.gif
using  NVelocity;
None.gif
using  NVelocity.App;
None.gif
using  NVelocity.Context;
None.gif
using  NVelocity.Runtime;
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 这个测试是基于NVelocity模板引擎实现的.
ExpandedBlockEnd.gif
/// </summary>
None.gif public   partial   class  NVelocity_模板引擎测试 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//创建NVelocity引擎的实例对象
InBlock.gif
        VelocityEngine velocity = new VelocityEngine();
InBlock.gif        
//初始化该实例对象
InBlock.gif
        ExtendedProperties props = new ExtendedProperties();
InBlock.gif        props.AddProperty(RuntimeConstants.RESOURCE_LOADER, 
"file");
InBlock.gif        props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
InBlock.gif        props.AddProperty(RuntimeConstants.INPUT_ENCODING, 
"gb2312");
InBlock.gif        props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, 
"gb2312");
InBlock.gif        velocity.Init(props); 
InBlock.gif        
//从文件中读取模板
InBlock.gif
        Template temp = velocity.GetTemplate("myTemplate.html");
InBlock.gif        IContext context 
= new VelocityContext();
InBlock.gif        context.Put(
"from""Sichuan");
InBlock.gif        context.Put(
"to""hainan");
InBlock.gif        context.Put(
"subject""welcome to nvelocity");
InBlock.gif        context.Put(
"name""McJeremy");
InBlock.gif        
//合并模板
InBlock.gif
        StringWriter writer = new StringWriter();
InBlock.gif        
//velocity.MergeTemplate(context, writer);
InBlock.gif
        temp.Merge(context, writer);
InBlock.gif        
//生成静态页
InBlock.gif
 using (StreamWriter writer2 = new StreamWriter(Server.MapPath("/"+ "test.html"), false, Encoding.UTF8, 200))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            writer2.Write(writer);
InBlock.gif            writer2.Flush();
InBlock.gif            writer2.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }
    
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/McJeremy/archive/2008/06/25/1229848.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值