1、
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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());
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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());
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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();
}
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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());
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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 >
< 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 >