迭代内置对象:  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. C#之发送邮件【模板】+【封装】ZJ版

    PS: 为了弥补上篇博客的不足,正好周六闲着没事.所以进行优化下,来个终结版 功能实现:模板发送+自指定邮箱发送+解耦 总体预览如下: 各代码如下:(代码略多,所以都折叠了) 前台; @{ Layou ...

  2. 数塔取数 基础dp

    从低端向上,每个结点取下一层左右结点最大值和本身价值相加,dp[0][0]为最后结果 #include<iostream> #include<algorithm> #inclu ...

  3. Redis系列之(二):Redis主从同步,读写分离

    1. Redis主从同步 Redis支持主从同步.数据可以从主服务器向任意数量的从服务器上同步,同步使用的是发布/订阅机制. 2. 配置主从同步 Mater Slave的模式,从Slave向Maste ...

  4. 八月22日,django知识点总结:

    八月22日,知识点总结: python manage.py makemigrations python manage.py migrate unique=true是指这个字段的值在这张表里不能重复,所 ...

  5. 开发socketserver 以及定制开发自己的FTP服务器

    socket server 示例 #服务端程序 import socketserver class TcpHandler(socketserver.BaseRequestHandler): def h ...

  6. Wpf usercontrol dispose

    窗口关闭时组件"析构": public UserControl()        {            InitializeComponent();               ...

  7. RN中listView的每个item等高

    今天写ListView的每个Item的布局的时候发现,当文字太长时被截掉了不能完全显示,检查了很久发现没有设置固定高度都是可伸缩的为什么没有伸缩呢.看了很久才发现每个item是等高的,于是仔细检查才看 ...

  8. iOS-RunLoop

    简单的说run loop是事件驱动的一个大循环,如下代码所示int main(int argc, char * argv[]) {     //程序一直运行状态     while (AppIsRun ...

  9. Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  10. python爬乌云dorps文章

    有时候翻看drops 无赖drops没有一个全部文章列表,所以就有了这个想法把所有文件标题链接都爬出来这样又直观又好找感兴趣的文章 #coding=utf-8 import re import url ...