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
總結
- 上一篇: 将一个项目中已有的文档添加到另一个项目中
- 下一篇: 有环单链表