前不久,接觸到.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())?
這個是直接輸入到頁面上。如果我們不直接輸出到頁面上,而是把它寫入到一個檔案中呢?
產生靜態頁--是的,這是讓大家都心動的。
下面的代碼是我第一個練習:using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
/**//// <summary>
/// 這個測試是基於NVelocity模板引擎實現的.
/// </summary>
public partial class NVelocity_模板引擎測試 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//建立NVelocity引擎的執行個體對象
VelocityEngine velocity = new VelocityEngine();
//初始化該執行個體對象
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
//可換成:props.AddProperty("resouce.loader","file"),以下的同道理
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
velocity.Init(props);
//從檔案中讀模數板
Template temp = velocity.GetTemplate("myTemplate.html");
IContext context = new VelocityContext();
context.Put("from", "Sichuan");
context.Put("to", "hainan");
context.Put("subject", "welcome to nvelocity");
context.Put("name", "McJeremy");
//合并模板
StringWriter writer = new StringWriter();
//velocity.MergeTemplate(context, writer);
temp.Merge(context, writer);
//輸入
Response.Write(writer.ToString().Replace("\r\n", "<br/>"));
}
}
以下是產生靜態頁的練習:using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
/**//// <summary>
/// 這個測試是基於NVelocity模板引擎實現的.
/// </summary>
public partial class NVelocity_模板引擎測試 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//建立NVelocity引擎的執行個體對象
VelocityEngine velocity = new VelocityEngine();
//初始化該執行個體對象
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
velocity.Init(props);
//從檔案中讀模數板
Template temp = velocity.GetTemplate("myTemplate.html");
IContext context = new VelocityContext();
context.Put("from", "Sichuan");
context.Put("to", "hainan");
context.Put("subject", "welcome to nvelocity");
context.Put("name", "McJeremy");
//合并模板
StringWriter writer = new StringWriter();
//velocity.MergeTemplate(context, writer);
temp.Merge(context, writer);
//產生靜態頁
using (StreamWriter writer2 = new StreamWriter(Server.MapPath("/") + "test.html"), false, Encoding.UTF8, 200))
{
writer2.Write(writer);
writer2.Flush();
writer2.Close();
}
}
}