NVelocity模版引擎

NVelocity模版引擎

需要引用外部的NVelocity.dll文件
///
///Person类 的摘要说明
///
public class Person
{
public Person()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public string Name { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}

示例1

后台代码

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;
using System.IO;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//使用模版引擎
//一。创建模版引擎
//1.初始化模版引擎
VelocityEngine vEngine = new VelocityEngine();
//2.设置模版引擎初始化属性
 vEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, context.Request.MapPath("~/NVelocity/temp/"));//设置模版引擎默认的模版所在的根路径
    //3.初始化模版引擎
    vEngine.Init();
    //二。设置模版中使用的数据,将需要在模版中使用的数据都加到VelocityContext上下文对象
    //1.创建一个VelocityContext上下文对象
    VelocityContext vContext = new VelocityContext();
    //2.向context对象中设置数据
    vContext.Put("page_title", "这是我的第一个模版引擎页面");
    Person p = new Person() { Name = "顾毓婷", Age = 28, Email = "gyt@163.com", Gender = "女" };
    vContext.Put("page_content", p);
    //三。合并模版引擎要加载的模版文件与VelocityContext对象,合并完成后模版引擎内部就会对模版与数据进行整合,并生成新的内容
    using(StringWriter strWriter=new StringWriter ())
    {
        vEngine.MergeTemplate("HTMLPage.htm", "UTF-8", vContext, strWriter);//第一个是模版的文件名
        //四。把生成好的新字符串响应给用户
        context.Response.Write(strWriter);
    }
}
public bool IsReusable {
    get {
        return false;
    }
}

}

前台模版页面代码

页面名字是HTMLPage.htm

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$page_title</title>
</head>
<body>
<ul>
    <li> 姓名:$page_content.Name</li>
     <li> 年龄:$page_content.Age</li>
      <li> 性别:$page_content.Gender</li>
       <li> 邮箱:$page_content.Email</li>
</ul>
</body>
</html>

示例2

后台代码

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;
using System.IO;
using System.Collections.Generic;
public class Handler2 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/html";
    //创建模版对象
    //1创建模版对象
    VelocityEngine vEngine = new VelocityEngine();
    //2设置属性
   vEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, context.Request.MapPath("~/NVelocity/temp/"));
    //3初始化
    vEngine.Init();
    //二。设置  VelocityContext  数据
    //1创建VelocityContext  对象
    VelocityContext vContext = new VelocityContext();
    List<Person> list=new List<Person> (){
    new Person(){Name="胡飞行",Age=19,Email="21311@163.com",Gender="男"},
    new Person(){Name="胡飞行",Age=19,Email="21311@163.com",Gender="男"},
    new Person(){Name="胡飞行",Age=19,Email="21311@163.com",Gender="男"},
    new Person(){Name="胡飞行",Age=19,Email="21311@163.com",Gender="男"},
    };
    //2设置数据
    vContext.Put("pList", list);
    //3合并模版文件与VelocityContext上下文对象
    using (StringWriter strWriter = new StringWriter()) {
        vEngine.MergeTemplate("HTMLPage2.htm", "utf-8", vContext, strWriter);
        //4向客户端写入
        context.Response.Write(strWriter);
    }
}
public bool IsReusable {
    get {
        return false;
    }
}
}

前台模版页面代码

页面的代码里不能有注释,这里是为了理解

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
    <thead>
    <tr>
    <th>姓名</th>
    <th>性别</th>
    <th>年龄</th>
    <th>邮箱</th>
    </tr>
    </thead>
    <tbody>
    #foreach($p in $pList)<!--代表循环-->
    #even<!--代表偶数行-->
    <tr style="background-color:Blue"><td>$p.Name</td><td>$p.Age</td><td>$p.Email</td><td>$p.Gender</td></tr>
    #odd<!--代表奇数行-->
     <tr style="background-color:Green"><td>$p.Name</td><td>$p.Age</td><td>$p.Email</td><td>$p.Gender</td></tr>
     #each<!--代表每行都-->
     <tr style="background-color:Gray"><td cplspan='4'></td></tr>
    #end
    </tbody>
</table>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值