Nvelocity 实现动态加载 例子

因为工作需要使用Nvelocity ,所以简单的做了个小例子

首先建立一个hmtl页面 然后添加  替换的脚本如红色的部分。注意$后边的字符不能事中文和独立的数字

<!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>
&nbsp;$m $q
</body>
</html>

然后添加一个aspx 页面 首先引用 Nvelocity.dll

然后


using System;
using System.Collections.Generic;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity.Context;
using Commons.Collections;
using System.IO;
using System.Text;


namespace loginDemo
{
    public partial class Nvelocity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }


//这个方法用于页面合并后打印到前台页面
        void MergeTemplate()
        {
            //创建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));
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
            velocity.Init(props);
            //从文件中读取模板
            Template temp = velocity.GetTemplate("template/index.htm");
            IContext context = new VelocityContext();
            context.Put("m", "亲");
            context.Put("q", "李坤然");


            //合并模板
            StringWriter writer = new StringWriter();
            temp.Merge(context, writer);
            //输入
            Response.Write(writer.ToString().Replace("\r\n", "<br/>"));
 
        }



//这个页面用于再本地根据模板生成相应的 html文件,cms中应该会用到

   void WriterTemplate

{


            //创建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));
        props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
        props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
        velocity.Init(props); 
        //从文件中读取模板
        Template temp = velocity.GetTemplate("template/index.htm");
        IContext context = new VelocityContext();
        context.Put("m", "亲");
        context.Put("q", "李坤然");
       
        //合并模板
        StringWriter writer = new StringWriter();
        
        temp.Merge(context, writer);
        //生成静态页
using (StreamWriter writer2 = new StreamWriter(Server.MapPath("/") + "test1.html",false,Encoding.UTF8))
        {
            writer2.Write(writer);
            writer2.Flush();
            writer2.Close();
        }

}




    }
}

遇见了就不要错过
原文地址:https://www.cnblogs.com/Traner/p/2819922.html