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
}
NVelocity的更多相关文章
- 模板引擎Nvelocity实例
前言 最近一直忙于工作,没时间来管理博客,同时电脑也不给力,坏了一阵又一阵,最后还是去给修理了,这不刚一回来迫不及待的就写一篇文章来满足两个月未写博客的紧迫感. Nvelocity 关于nveloci ...
- NVelocity解析字符串
之前都是先从模板文件里面读取html字符串,现在要求将模板存入数据库或缓存了,怎么办呢?在网上找了下资料,终于找到解决办法. 如下: public class NVelocityHelper { // ...
- NVelocity介绍,NVelocity中文手册文档及实例下载
NVelocity是什么velocity英音:[vi'lɔsiti]美音:[və'lɑsətɪ]近在做一个项目,客户要求有网站模板功能,能够自主编辑网站的风格,因为这个系统是为政府部门做子站系统,举个 ...
- NVelocity用法(转)
每个人应该知道的NVelocity用法 NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来 ...
- Nvelocity用法
NVelocity用法 NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定 ...
- 【转】NVelocity模板引擎初学总结
转自:http://sunxitao88.blog.163.com/blog/static/68314439200861963326251/ 前不久,接触到.NET下的MVC-MonoRail,它推荐 ...
- 使用NVelocity生成内容的几种方式
使用NVelocity也有几个年头了,主要是在我的代码生成工具Database2Sharp上使用来生成相关代码的,不过NVelocity是一个非常不错的模板引擎,可以用来生成文件.页面等相关处理,非常 ...
- 抛弃NVelocity,来玩玩Razor
对于内容型,不易变动的东西我们都希望给它来个静态化,还有种情况就是比如新浪云不支持.net,为了能跑起我们的网站, 只能放些静态页面上面,外加jsonp来实现交互,我们知道.net中有很多模板引擎,但 ...
- Asp.net NVelocity 模版引擎
NVelocity.dll是Java中常用的一个模版,下面是常用的模版引擎 1,返回string类型的html代码 /// <summary> /// 获取html模版 /// </ ...
随机推荐
- sstream
sstream用法 #include<iostream> #include<sstream> #include<string> using namespace st ...
- apache.commons.compress 压缩,解压
最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...
- Ubuntu下deb文件及tgz文件安装
dpkg 是Debian Package的简写,是为Debian 专门开发的套件管理系统,方便软件的安装.更新及移除.所有源自Debian的Linux发行版都使用dpkg,例如Ubuntu.Knopp ...
- 利用django创建一个投票网站(四)
创建你的第一个 Django 项目, 第四部分 这一篇从第三部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,专注于简单的表单处理并且精简我们的代码. 编写一个简单的表单 让我们更新一下在上一个 ...
- 网页访问全过程剖析[转].md
本文转载自:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文翻译自:http://igoro.com/archive/ ...
- php获得远程信息到本地使用的3个函数:file_get_contents和curl函数和stream_get_contents
1:file_get_contents echo file_get_contents("http://www.php.com/index.php"); 2:curl funct ...
- thinkphp添加数据
<?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...
- 20145212&20145204信息安全系统实验四报告
一.实验内容与步骤 1.本次实验建立在掌握嵌入式开发平台使用方法和配置方法的基础上,要求使用windows xp,linux(red hat),arm三个系统(即NFS方式): 2.在linux系统中 ...
- 前端Js框架汇总
概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人心,.net co ...
- asp.net core输出中文乱码的问题
摘要 在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 问题重现 新建控制台和站点 public class Program { public static ...