迭代内置对象:  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的更多相关文章

  1. 模板引擎Nvelocity实例

    前言 最近一直忙于工作,没时间来管理博客,同时电脑也不给力,坏了一阵又一阵,最后还是去给修理了,这不刚一回来迫不及待的就写一篇文章来满足两个月未写博客的紧迫感. Nvelocity 关于nveloci ...

  2. NVelocity解析字符串

    之前都是先从模板文件里面读取html字符串,现在要求将模板存入数据库或缓存了,怎么办呢?在网上找了下资料,终于找到解决办法. 如下: public class NVelocityHelper { // ...

  3. NVelocity介绍,NVelocity中文手册文档及实例下载

    NVelocity是什么velocity英音:[vi'lɔsiti]美音:[və'lɑsətɪ]近在做一个项目,客户要求有网站模板功能,能够自主编辑网站的风格,因为这个系统是为政府部门做子站系统,举个 ...

  4. NVelocity用法(转)

    每个人应该知道的NVelocity用法   NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来 ...

  5. Nvelocity用法

    NVelocity用法 NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定 ...

  6. 【转】NVelocity模板引擎初学总结

    转自:http://sunxitao88.blog.163.com/blog/static/68314439200861963326251/ 前不久,接触到.NET下的MVC-MonoRail,它推荐 ...

  7. 使用NVelocity生成内容的几种方式

    使用NVelocity也有几个年头了,主要是在我的代码生成工具Database2Sharp上使用来生成相关代码的,不过NVelocity是一个非常不错的模板引擎,可以用来生成文件.页面等相关处理,非常 ...

  8. 抛弃NVelocity,来玩玩Razor

    对于内容型,不易变动的东西我们都希望给它来个静态化,还有种情况就是比如新浪云不支持.net,为了能跑起我们的网站, 只能放些静态页面上面,外加jsonp来实现交互,我们知道.net中有很多模板引擎,但 ...

  9. Asp.net NVelocity 模版引擎

    NVelocity.dll是Java中常用的一个模版,下面是常用的模版引擎 1,返回string类型的html代码 /// <summary> /// 获取html模版 /// </ ...

随机推荐

  1. Android开发之import org.apache.http

    在build.gradle(app)中添加一下依赖: android { useLibrary 'org.apache.http.legacy' } dependencies { compile 'o ...

  2. NCspider项目总结

    下午就要答辩了,想把项目经历再总结一下. 项目分三个阶段. 第一阶段,是信息搜集整理阶段 要想法设法从各个门户网站上抓取到新闻和对应的评论数据.首先要分析网站结构. 1. 从哪里找到网站每日发布的所有 ...

  3. UDP通信

    package com.slp; import java.io.IOException; import java.net.DatagramPacket; import java.net.Datagra ...

  4. 使用UITableView展示数据

    TableView主要用于展示数据,类似于Android中的ListView. 我们可以通过两个方式使用TableView.第一种是直接使用TableView类.第二种是通过UITableViewCo ...

  5. Spring缓存机制的理解

    在spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果:2.在某个方法执行前或后清空缓存. 下面写两个类来模拟Spring的缓存机制: package com.sin90lzc ...

  6. 篇二:MySQL存储过程

    目的:写一个存储过程,往数据库中插入几百条数据,作为识别码给别人用(这里我觉得和验证码的功能相似) BEGIN ); ); ) ; ); ); ; ; while count <= insert ...

  7. Keepalived的安装

    Keepalived的安装 官网下载:点击直达 yum install -y libnl yum install -y libnl-devel 下载 cd /usr/local/src/ wget h ...

  8. Tomcat内存溢出的三种情况及解决办法分析

    Tomcat内存溢出的原因 在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一样的,当然处理方式也不一样. 这里根据平时遇到的情况和相关资料进行一个总结.常见的一般会有下面三种 ...

  9. Beta阶段第九次Scrum Meeting

    情况简述 BETA阶段第九次Scrum Meeting 敏捷开发起始时间 2017/1/2 00:00 敏捷开发终止时间 2017/1/3 00:00 会议基本内容摘要 deadline临近 参与讨论 ...

  10. 【Phylab2.0】Beta版本项目展示

    团队成员 冯炜韬(PM)http://www.cnblogs.com/toka 岳桐宇(后端)http://www.cnblogs.com/mycraftmw 杨子琛(测试&LaTeX)htt ...