迭代内置对象:  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. 前端小知识点---html换行被解析为空格的相关知识

    这个系列主要记录一下常被忽略但又经常产生影响的知识点,纯做个记录,方便查询 html换行被解析为空格也是常说的3像素空隙的问题,根据测试不同浏览器产生的空隙大小会不一样,Chrome,Firefox, ...

  2. Android系统中应用的安装和卸载的监听

    一.创建一个类继承BroadcastReceiver并且复写onReceive的方法 public class AppStateReceiver extends BroadcastReceiver { ...

  3. python实战(开发新浪微博应用)

    #coding=utf-8 """ 本应用主要功能 1.用户选择喜欢的标签加关注 2.获取用户粉丝中自己还没有关注的,->加关注,提高粉丝稳定性 3.获取用户关注列 ...

  4. 一次kubernetes资源文件创建失败的排查

    今天在jenkins中创建kubernetes的rc时,检查目标yaml文件时报出如下错误: + /opt/jenkins/kube/kubectl -s http://10.xx.xx.xx:808 ...

  5. JAVA面向对象

    JAVA面向对象 对象   我们生活中能看到能摸到的一切事物都是对象.在程序中模拟出生活中的所有东西万物皆对象   只要是对象--属性和行为(方法)   属性   对象有什么   例如:学生有姓名.学 ...

  6. BZOJ 1221: [HNOI2001] 软件开发

    1221: [HNOI2001] 软件开发 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1428  Solved: 791[Submit][Stat ...

  7. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  8. 在页面的el表达式是如何判断null的

    <c:if test="${not empty message}"> <div id="message" class="alert ...

  9. jsp上传图片实时显示

    jsp代码 <div class="form-group" id="caseIma"> <label class="control- ...

  10. iOS - URL Scheme 操作

    推荐JLRoutes路由跳转 NSScanner 在寻找更加灵活的页面跳转和通知,我遇见了JLRoutes,从而学习使用URL Scheme来定义界面入口.以前从来没有使用过,不过很多大厂和流行的框架 ...