NVelocity在web開發中是經常使用的,它可以同時開發一個遵循MVC架構的web站點,接下來錯新技術頻道小編就給大家介紹NVelocity內容的生成方式,希望對你有所幫助。
使用NVelocity也有幾個年頭了,主要是在我的代碼生成工具Database2Sharp上使用來生成相關代碼的,不過NVelocity是一個非常不錯的模板引擎,可以用來生成文件、頁面等相關處理,非常高效和方便。
1、基于NVelocity的幾種內容生成方式

從上面的圖示,我們可以看到,NVelocity的模板化生成包含了3種方式,一種是從文件到文件或者字符串,一種是從字符串到字符串,他們各自的處理方式有所不同,但是都能正確解析里面的內容。
為了更好利用NVelocity的特性,我們對它進行一個初步的輔助類封裝。
/// <summary>/// 基于NVelocity的模板文件生成輔助類/// </summary>public class NVelocityHelper{protected VelocityContext context;protected Template template;protected string templateFile;/// <summary>/// 存放鍵值的字典內容/// </summary>private Dictionary<string, object> KeyObjDict = new Dictionary<string, object>();/// <summary>/// 添加一個鍵值對象/// </summary>/// <param name="key">鍵,不可重復</param>/// <param name="value">值</param>/// <returns></returns>public NVelocityHelper AddKeyValue(string key, object value){if (!KeyObjDict.ContainsKey(key)){KeyObjDict.Add(key, value);}return this;}................上面的AddKeyValue方法,主要用來為模板引擎添加一些需要綁定在頁面上的變量對象,這樣頁面變量參數的內容就能正確解析出來了。
為了使用NVelocity的各種特性,我們需要在輔助類里面構造模板的相關信息,設置相關參數。
/// <summary>/// 初始化模板引擎/// </summary>protected virtual void InitTemplateEngine(){try{//Velocity.Init(NVELOCITY_PROPERTY);VelocityEngine templateEngine = new VelocityEngine();templateEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");templateEngine.SetProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");templateEngine.SetProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");//如果設置了FILE_RESOURCE_LOADER_PATH屬性,那么模板文件的基礎路徑就是基于這個設置的目錄,否則默認當前運行目錄templateEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory);templateEngine.Init();template = templateEngine.GetTemplate(templateFile);}catch (ResourceNotFoundException re){string error = string.Format("Cannot find template " + templateFile);LogTextHelper.Error(error);throw new Exception(error, re);}catch (ParseErrorException pee){string error = string.Format("Syntax error in template " + templateFile + ":" + pee.StackTrace);LogTextHelper.Error(error);throw new Exception(error, pee);}}在生成內容之前,需要把相關的對象屬性綁定到模板引擎的上下文對象里面。
/// <summary>/// 初始化上下文的內容/// </summary>private void InitContext(){context = new VelocityContext();foreach (string key in KeyObjDict.Keys){context.Put(key, KeyObjDict[key]);}}1)根據模板文件構造對應的文件內容
/// <summary>///根據模板創建輸出的文件,并返回生成的文件路徑/// </summary>public virtual string ExecuteFile(){string fileName = "";if (template != null){string filePath = CheckEndBySlash(directoryOfOutput);fileName = filePath + fileNameOfOutput + fileExtension;if (!string.IsNullOrEmpty(filePath) && !Directory.Exists(filePath)){Directory.CreateDirectory(filePath);}//LogTextHelper.Debug(string.Format("Class file output path:{0}", fileName));InitContext();using (StreamWriter writer = new StreamWriter(fileName, false)){template.Merge(context, writer);}}return fileName;}2)根據模板文件構造字符串內容
/// <summary>/// 根據模板輸出字符串內容/// </summary>/// <param name="templateFile"></param>/// <returns></returns>public string ExecuteString(){InitContext(); System.IO.StringWriter writer = new System.IO.StringWriter();template.Merge(context, writer);return writer.GetStringBuilder().ToString();}3)根據字符串內容構造字符串輸出
/// <summary>/// 合并字符串的內容/// </summary>/// <returns></returns>public string ExecuteMergeString(string inputString){VelocityEngine templateEngine = new VelocityEngine();templateEngine.Init();InitContext();System.IO.StringWriter writer = new System.IO.StringWriter();templateEngine.Evaluate(context, writer, "mystring", inputString);return writer.GetStringBuilder().ToString();}上面幾種操作模板輸出的方式,其調用代碼如下所示。
private void btnGenerateFile_Click(object sender, EventArgs e){string tempalte = "Template/template.htm";//相對目錄TestInfo info = new TestInfo();info.Title = "測試標題";info.Content = "測試內容,這是測試內容";info.Datetime = DateTime.Now;NVelocityHelper adapter = new NVelocityHelper(tempalte);adapter.AddKeyValue("title", "This is a title").AddKeyValue("content", "This is a Content").AddKeyValue("datetime", System.DateTime.Now).AddKeyValue("TestInfo", info);adapter.FileNameOfOutput = "testTemplate";string filePath = adapter.ExecuteFile();if (!string.IsNullOrEmpty(filePath)){Process.Start(filePath);}} private void btnGenerate_Click(object sender, EventArgs e){string tempalte = "Template/template.htm";//相對目錄TestInfo info = new TestInfo();info.Title = "測試標題";info.Content = "測試內容,這是測試內容";info.Datetime = DateTime.Now;NVelocityHelper adapter = new NVelocityHelper(tempalte);adapter.AddKeyValue("title", "This is a title").AddKeyValue("content", "This is a Content").AddKeyValue("datetime", System.DateTime.Now).AddKeyValue("TestInfo", info);this.txtCode.Text = adapter.ExecuteString();} private void btnMergeString_Click(object sender, EventArgs e){System.Text.StringBuilder builder = new System.Text.StringBuilder();builder.Append("${Title}/r/n" +"$Content/r/n" +"$Digest/r/n" +"$Author/r/n" +"$Keyword/r/n" +"$DateTime/r/n");NVelocityHelper adapter = new NVelocityHelper();adapter.AddKeyValue("Title", "標題").AddKeyValue("Content", "內容").AddKeyValue("Digest", "摘要").AddKeyValue("Author", "作者").AddKeyValue("Keyword", "關鍵詞").AddKeyValue("DateTime", DateTime.Now);this.txtCode.Text = adapter.ExecuteMergeString(builder.ToString());}2、模板引擎NVelocity的幾種應用場景
上面的幾種操作模板內容的方式,能夠在絕大多數情況下滿足我們的應用要求,如可以在代碼生成工具里面,定義一些自定義的內容模板,然后結合數據庫的元數據信息,實現豐富邏輯的代碼生成操作。

也可以在一些內容管理的應用上(如文章管理方面),根據輸入的內容,實現文章內容的文件生成操作,這個生成后,我們就直接使用文章的文件鏈接地址就可以了。

或者根據數據信息生成具體的頁面,用于套打操作,如下是Winform里面的套打處理。


通過錯新技術頻道小編介紹的NVelocity內容的生成方式,相信大家已經明白了,更多的專業信息盡在錯新技術頻道。
新聞熱點
疑難解答
圖片精選