NVelocity模板引擎,初级体验,非常有用的东东
2009-03-26 14:16
387 查看
首先:在http://nvelocity.sourceforge.net/上下载一个 NVelocity-0.4.1.zip
在其下的bin目录中可找到NVelocity.dll(NET项目中将用到),引入项目中....
先要引入以下名称空间:
using Commons.Collections;
using
NVelocity;
using NVelocity.App;
using NVelocity.Context;
第一步:Creating a
VelocityEngine也就是创建一个VelocityEngine的实例
VelocityEngine velocity = new VelocityEngine(); //也可以使用带参构造函数直接实例。
ExtendedProperties
props = new
ExtendedProperties();
velocity.Init(props);
第二步:Creating the
Template加载模板文件
这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
Template
template = velocity.GetTemplate(@"path/to/myfirsttemplate.vm");
第三步:Merging the
template整合模板
VelocityContext context = new
VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject",
"Welcome to NVelocity");
context.Put("customer", new Customer("John Doe")
);
第四步:创建一个IO流来输出模板内容。推荐使用StringWriter(因为template中以string形式存放)
StringWriter
writer = new StringWriter();
template.Merge(context,
writer);
Response.Write(writer.ToString());
---通过上述步骤就可以轻松的使用NVelocity模板引擎的技术了。
有没有发现最后的Response.Write(writer.ToString())?
在其下的bin目录中可找到NVelocity.dll(NET项目中将用到),引入项目中....
先要引入以下名称空间:
using Commons.Collections;
using
NVelocity;
using NVelocity.App;
using NVelocity.Context;
第一步:Creating a
VelocityEngine也就是创建一个VelocityEngine的实例
VelocityEngine velocity = new VelocityEngine(); //也可以使用带参构造函数直接实例。
ExtendedProperties
props = new
ExtendedProperties();
velocity.Init(props);
第二步:Creating the
Template加载模板文件
这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
Template
template = velocity.GetTemplate(@"path/to/myfirsttemplate.vm");
第三步:Merging the
template整合模板
VelocityContext context = new
VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject",
"Welcome to NVelocity");
context.Put("customer", new Customer("John Doe")
);
第四步:创建一个IO流来输出模板内容。推荐使用StringWriter(因为template中以string形式存放)
StringWriter
writer = new StringWriter();
template.Merge(context,
writer);
Response.Write(writer.ToString());
---通过上述步骤就可以轻松的使用NVelocity模板引擎的技术了。
有没有发现最后的Response.Write(writer.ToString())?
相关文章推荐
- NVelocity模板引擎,初级体验,非常有用的东东.(转)
- Velocity模板引擎入门
- Java的模板引擎Velocity初体验
- 【Velocity模板引擎】前端页面#if...#elseif...#end语句用法
- 基于java的Velocity模板匹配引擎
- Java模板引擎---Velocity
- Velocity模板引擎的缓冲机制
- Velocity模板引擎使用笔记
- velocity模板引擎学习
- Velocity模板引擎下 velocity.properties转义(转码)配置
- JAVA velocity模板引擎使用实例
- 记录一个 Velocity 模板引擎的缓存问题
- Handlerbars.js模板引擎的使用之初级篇
- Velocity模板引擎
- 全面超越 Velocity 的全新一代 Java 模板引擎 jetbrick-template 1.0.2 正式版发布。
- Java模板引擎Velocity 2007年3月底
- JAVA velocity模板引擎使用实例
- Velocity模板引擎的简单使用
- sdcms的模板解析引擎,一个非常简单和实用的CMS
- Jsp、Velocity、FreeMarker这三种模板引擎的区别