在一般处理程序中,如果我们要用Context.Response.Write(string) 来输出页面,如果不用到模板引擎,直接写HTML代码,则会显得非常的繁琐和不便,而是用了模板引擎,则事半功倍,
下面就介绍NVelocity 其工作步骤 : 编写模板 --》提供数据 --》渲染成HTML 利用Context.Response.Write(string) 来输出,
下面是简单的代码演示
在一般处理程序中 ,一个简单的登录功能,
public class Login2 : IHttpHandler
{
//采用NVelocity 模板引擎 longin 可改变为
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string userName = context.Request["username"];
string passWord = context.Request["password"];
#region Nvelocity 的用法 传入普通的值
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(passWord))
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/aa"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("username", "");//设置参数,在模板中可以通过$data来引用
vltContext.Put("password", "");
vltContext.Put("msg", "");
Template vltTemplate = vltEngine.GetTemplate("login.htm");
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
else
{
if (userName != "admin" || passWord != "123")
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/aa"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("username", userName);//设置参数,在模板中可以通过$data来引用
vltContext.Put("password", passWord);
vltContext.Put("msg", "err");
Template vltTemplate = vltEngine.GetTemplate("login.htm");
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
else
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/aa"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("username", userName);//设置参数,在模板中可以通过$data来引用
vltContext.Put("password", passWord);
vltContext.Put("msg", "right");
Template vltTemplate = vltEngine.GetTemplate("login.htm");
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
}
#endregion
}
public bool IsReusable
{
get
{
return false;
}
}
}
在程序集中 aa 文件夹下有 Login.htm 文件 用 $ 符标记传入的变量
<!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></title>
</head>
<body>
<strong><font color='red'>登录</font></strong>
<form action='/Login2.ashx'>
<input type='text' name='username' value='$username'/>
<input type='password' name='password' value='$password' />
<input type='submit' value='登录'/>
</form>
<p>$msg</p>
</body>
</html>
当然 NVelocity 中也可传入 对象
如下
public class Login2 : IHttpHandler
{
//采用NVelocity 模板引擎 longin 可改变为
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
#region Nvelocity 可以传输对象到页面模板
Person per = new Person();
per.Name = "zhang";
per.Age = "30";
per.Father = new Person() { Name = "lili", Age = "23" };
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/aa"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("p", per);//设置参数,在模板中可以通过$data来引用
Template vltTemplate = vltEngine.GetTemplate("test.htm");
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
#endregion
}
public bool IsReusable
{
get
{
return false;
}
}
}
其对应的html 页面为
<!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></title>
</head>
<body>
姓名是:$p.Name 年龄是 :$p.Age 父亲是:$p.Father.Name
<!-- $dic.zhangcheng-->
</body>
</html>
见代码
public class Login2 : IHttpHandler
{
//采用NVelocity 模板引擎 longin 可改变为
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string userName = context.Request["username"];
string passWord = context.Request["password"];
#endregion
#region “$data.索引器值 如键值对 可以用,还有就是datarow 可以用
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["tom"] = "qinghua";
dic["zhangcheng"] = "nanada";
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/aa"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("dic", dic);//设置参数,在模板中可以通过$data来引用
Template vltTemplate = vltEngine.GetTemplate("test.htm");
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
#endregion
}
html 代码如上
通过 Nvelociyt 还可以在模板中使用 循环,判断 ,看对应的HTML 代码
<!--#parse("head.htm")-->
#include("head.htm")
<!--<ul>
#foreach($p in $persons)
<li>$p.Name</li>
#end
</ul>-->
<ul>
#foreach($p in $persons)
#if($p.Age == "30")
<li>$p.Name</li>
#end
#end
</ul>
</body>
</html>