日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NVelocity

發布時間:2024/4/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NVelocity 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

迭代內置對象:? velocityCount

集合數   ? : ?count

?

NVelocity遇到不能處理的引用時,一般會直接輸出標簽名稱。

?

在$符號后加個!號,出現Null時,標簽的內容就會顯示空白。

?

如:$title?? 改寫成:$!{title}

?

/// <summary>
??? /// 黎巧
??? /// 2012-04-25
??? ///? NVelocity模板工具類 VelocityHelper
??? /// </summary>
??? public class NVelocityHelper
??? {
??????? private VelocityEngine velocity = null;
??????? private IContext context = null;

??????? /// <summary>
??????? /// 默認構造函數
??????? /// </summary>
??????? public NVelocityHelper()
??????? {
??????????? Init(AppDomain.CurrentDomain.BaseDirectory);
??????? }

??????? /// <summary>
??????? /// 構造函數
??????? /// </summary>
??????? /// <param name="templatePath">資源加載路徑</param>
??????? public NVelocityHelper(string templatePath)
??????? {
??????????? Init(templatePath);
??????? }

??????? /// <summary>
??????? /// 初始話NVelocity模塊
??????? /// </summary>
??????? /// <param name="templatDir">模板文件夾所在目錄</param>
??????? public void Init(string templatDir)
??????? {
??????????? //創建VelocityEngine實例對象
??????????? velocity = new VelocityEngine();
??????????? //使用設置初始化VelocityEngine
??????????? ExtendedProperties props = new ExtendedProperties();
??????????? props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
??????????? props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatDir);//存放模板文件的目錄
??????????? props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
??????????? props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
??????????? velocity.Init(props);

??????????? //為模板變量賦值
??????????? context = new VelocityContext();
??????????? //context.Put("formatter", new VelocityFormatter(context));
??????? }

??????? /// <summary>
??????? /// 給模板變量賦值
??????? /// </summary>
??????? /// <param name="key">模板變量</param>
??????? /// <param name="value">模板變量值</param>
??????? public void PutSet(string key, object value)
??????? {
??????????? if (context == null)
??????????? {
??????????????? context = new VelocityContext();

??????????? }
??????????? context.Put(key, value);
??????? }

??????? /// <summary>
??????? /// 生成html文件
??????? /// </summary>
??????? /// <param name="templateFileName">模板文件</param>
??????? /// <param name="htmlFileName">生成的html文件</param>
??????? public void Save(string templateFileName, string htmlFileName)
??????? {
??????????? Template template = velocity.GetTemplate(templateFileName, "UTF-8");
??????????? StringWriter sw = new StringWriter();
??????????? template.Merge(context, sw);

??????????? FileInfo file = new FileInfo(htmlFileName);
??????????? DirectoryInfo info = new DirectoryInfo(file.DirectoryName);
??????????? if (!info.Exists)
??????????? {
??????????????? info.Create();
??????????? }

??????????? using (StreamWriter writer = new StreamWriter(htmlFileName))
??????????? {
??????????????? writer.Write(sw);
??????????? }
??????? }

??????? /// <summary>
??????? /// 顯示模板
??????? /// </summary>
??????? /// <param name="templatFileName">模板文件名</param>
??????? public void Display(string templatFileName)
??????? {
??????????? //從文件中讀取模板
??????????? //Template template = velocity.GetTemplate(templatFileName);
??????????? Template template = velocity.GetTemplate(templatFileName, "UTF-8");
??????????? //合并模板
??????????? StringWriter writer = new StringWriter();
??????????? template.Merge(context, writer);
??????????? //輸出
??????????? //HttpContext.Current.Response.Clear();
??????????? //HttpContext.Current.Response.Write(writer.ToString());
??????????? //HttpContext.Current.Response.Flush();
??????????? //HttpContext.Current.Response.End();
??????? }

??????? #region 使用方法:
??????? /*
??????? VelocityHelper vh = new VelocityHelper();
??????? vh.Init(@"templates");//指定模板文件的相對路徑
??????? vh.PutSet("title", "員工信息");
??????? vh.PutSet("comName","成都xxxx里公司");
??????? vh.PutSet("property”,"天營");
??????? ArrayList aems = new ArrayList();
??????? //使用tp1.htm模板顯示
??????? vh.Display("tp1.htm");
??????? */
??????? #endregion
??? }

轉載于:https://www.cnblogs.com/liqiao/p/2475048.html

總結

以上是生活随笔為你收集整理的NVelocity的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。