NVelocity 入门使用

NVelocity是有名的网页模板引擎,代码为参考过来的,在此记录下。

NVelocity使用类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;

namespace NVelocityDemo
{
    public class NVelocityHelper
    {
        /// <summary>
        /// 生成网页模板与数据结合后的完整网面代码
        /// </summary>
        /// <param name="filename">模板文件名称</param>
        /// <param name="dictionary">页面数据的键值对</param>
        /// <returns></returns>
        public static string CreateHtmlCode(string filename, Dictionary<string,object> dictionary)
        {
            //第一步:初始化模板引擎
            VelocityEngine vltEngine = new VelocityEngine();
            //模板属性设置
            Commons.Collections.ExtendedProperties attri = new Commons.Collections.ExtendedProperties();
            //设置资源加载类型为文件类型
            attri.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            //设置模板文件存放路径
            attri.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/html"));
            attri.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
            attri.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
            vltEngine.Init(attri);

            //第二步:设置模板数据集            
            VelocityContext vltContent = new VelocityContext();
            //设置参数,在模板中可以通过$data.title或者$data.name来访问
            //循环添加参数值到前端页面
            foreach (KeyValuePair<string, object> kv in dictionary)
            {
                vltContent.Put(kv.Key, kv.Value);
            }

            //第三步:设置模板
            Template vltTemplate = vltEngine.GetTemplate(filename);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContent, vltWriter);
            string html = vltWriter.GetStringBuilder().ToString();
            return html;
        }
    }
}

 

html模板页(index.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>$data.title</title>
    <style type="text/css">
    *{ margin:0; padding:0;}
    </style>
</head>
<body>
    #include("header.htm")
    <h1>$data.content</h1>
    <ul>
        #foreach($u in $user)
            #if($u=="刘德华")
            <li style="color:Purple; font-weight:bold;">$u</li>
            #else
            <li>$u</li>
            #end
        #end
    </ul>
    <a href="/ashx/aboutus.ashx">关于我们</a>
    #include("footer.htm")
</body>
</html>

一般处理文件index.ashx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;

namespace NVelocityDemo.ashx
{
    /// <summary>
    /// index 的摘要说明
    /// </summary>
    public class index : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //第一组数据
            var data = new { title = "这里是文章标题", content = "这里是文章内容" };
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("data",data);
            
            //第二组数据
            string[] userlist = { "李谷一", "刘德华", "张铁", "任贤齐" };
            dic.Add("user", userlist);

            //生成模板页面
            string html = NVelocityHelper.CreateHtmlCode("index.htm", dic);
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

posted @ 2015-04-05 09:58  从小就喜欢编程  阅读(258)  评论(0)    收藏  举报