码迷,mamicode.com
首页 > 其他好文 > 详细

NVelocity

时间:2014-05-25 22:17:52      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:class   c   ext   http   a   get   

迭代内置对象:  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
    }

NVelocity,布布扣,bubuko.com

NVelocity

标签:class   c   ext   http   a   get   

原文地址:http://www.cnblogs.com/liqiao/p/2475048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!