NVelocity的几种写法

1、

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
IList < UserInfo > listUsers = new List < UserInfo > ();

UserInfo objUser
= new UserInfo();
objUser.Name
= " 测试员一 " ;
objUser.Sex
= " " ;
objUser.City
= " 深圳 " ;
listUsers.Add(objUser);

objUser
= new UserInfo();
objUser.Name
= " 测试员二 " ;
objUser.Sex
= " " ;
objUser.City
= " 北京 " ;
listUsers.Add(objUser);

objUser
= new UserInfo();
objUser.Name
= " 测试员三 " ;
objUser.Sex
= " " ;
objUser.City
= " 非洲 " ;
listUsers.Add(objUser);

IList
< string > listAssembly = new List < string > ();
// 添加程序集名称
listAssembly.Add( " LibTest " );

VelocityEngine vltEngine
= new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER,
" assembly " );
vltEngine.SetProperty(
" assembly.resource.loader.class " , " NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader; NVelocity " ); // 固定写法- -!
vltEngine.SetProperty( " assembly.resource.loader.assembly " , listAssembly);
vltEngine.Init();

VelocityContext vltContext
= new VelocityContext();
vltContext.Put(
" PageTitle " , " 资源模板例子 " );
vltContext.Put(
" ListUsers " , listUsers);

Template vltTemplate
= vltEngine.GetTemplate( " LibTest.Resources.Default.htm " );
System.IO.StringWriter vltWriter
= new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);

Response.Write(vltWriter.GetStringBuilder().ToString());

2、

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
// 创建NVelocity引擎的实例对象
VelocityEngine velocity = new VelocityEngine();
// 初始化该实例对象
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER,
" file " );
// 可换成:props.AddProperty("resouce.loader","file"),以下的同道理
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath( " ~/Template/ " ));
props.AddProperty(RuntimeConstants.INPUT_ENCODING,
" gb2312 " );
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING,
" gb2312 " );
velocity.Init(props);
// 从文件中读取模板
Template temp = velocity.GetTemplate( " Default.htm " );

IContext vltContext
= new VelocityContext();
vltContext.Put(
" PageTitle " , " 资源模板例子 " );
vltContext.Put(
" ListUsers " , listUsers);
// 合并模板
StringWriter writer = new StringWriter();
// velocity.MergeTemplate(context, writer);
temp.Merge(vltContext, writer);
// 输入
Response.Write(writer.GetStringBuilder().ToString());

 

3、生成静态文件abc.htm

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
// 创建NVelocity引擎的实例对象
VelocityEngine velocity = new VelocityEngine();
// 初始化该实例对象
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER,
" file " );
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath)
+ " /Template/ " );
props.AddProperty(RuntimeConstants.INPUT_ENCODING,
" gb2312 " );
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING,
" gb2312 " );
velocity.Init(props);
// 从文件中读取模板
Template temp = velocity.GetTemplate( " Default.htm " );
VelocityContext vltContext
= new VelocityContext();
vltContext.Put(
" PageTitle " , " 资源模板例子 " );
vltContext.Put(
" ListUsers " , listUsers);
// 合并模板
StringWriter writer = new StringWriter();
// velocity.MergeTemplate(context, writer);
temp.Merge(vltContext, writer);

// Template vltTemplate = velocity.GetTemplate("LibTest.Resources.Default.htm");
// System.IO.StringWriter vltWriter = new System.IO.StringWriter();
// temp.Merge(vltContext, vltWriter);
// 生成静态页
using (StreamWriter writer2 = new StreamWriter(Server.MapPath( "" ) + " /abc.htm " , false , Encoding.UTF8, 200 ))
{
writer2.Write(writer);
writer2.Flush();
writer2.Close();
}

 

4、

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER,
" file " );
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath(
"" ));
vltEngine.Init();

VelocityContext vltContext
= new VelocityContext();
vltContext.Put(
" PageTitle " , " 文件模板例子 " );
vltContext.Put(
" ListUsers " , listUsers);
vltContext.Put(
" ArticleManage " , new ArticleManage());

Template vltTemplate
= vltEngine.GetTemplate( " Default.htm " );
System.IO.StringWriter vltWriter
= new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);

Response.Write(vltWriter.GetStringBuilder().ToString());

 

5、模版文件Default.htm为:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head >
< title > $PageTitle </ title >
</ head >
< body >
#
foreach ($u in $ListUsers)
#beforeall
用户列表
< br />
---< br />
< table border = " 0 " cellpadding = " 10 " cellspacing = " 10 " >
< tr >
< td > 昵称 </ td >
< td > 性别 </ td >
< td > 城市 </ td >
</ tr >
#each
< tr >
< td > $u.Name </ td >
< td > $u.Sex </ td >
< td > $u.City </ td >
</ tr >
#afterall
</ table >
#nodata
暂无用户资料
#end

< img src = " ../image/WilsonBldg.jpg " />
</ body >
</ html >

 

转载于:https://www.cnblogs.com/why520crazy/articles/1681812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值