NVelocity模板引擎(生成静态页例子)
这个是直接输入到页面上。如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
生成静态页--是的,这是让大家都心动的。
下面的代码是我第一个练习:
 
生成静态页--是的,这是让大家都心动的。
下面的代码是我第一个练习:
using Commons.Collections;
 using NVelocity;
 using NVelocity.App;
 using NVelocity.Context;
 using NVelocity.Runtime;
 /// 
///这个测试是基于NVelocity模板引擎实现的.
 /// 
publicNVelocity_模板引擎测试 : System.Web.UI.Page
   {
      protected Page_Load( object sender, EventArgs e)
         {
              //创建NVelocity引擎的实例对象
        VelocityEngine velocity         = VelocityEngine();
               //初始化该实例对象
        ExtendedProperties props         = ExtendedProperties();
        props.AddProperty(RuntimeConstants.RESOURCE_LOADER,         "file");
          //可换成:props.AddProperty("resouce.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( "myTemplate.html");
        IContext context         = VelocityContext();
        context.Put(        "from", 
        context.Put(       "to", 
        context.Put(       "subject", to nvelocity  ");
        context.Put(       "name", 
              //合并模板
        StringWriter writer         = StringWriter();
               //velocity.MergeTemplate(context, writer); 
        temp.Merge(context, writer);
                      //输入
        Response.Write(writer.ToString().Replace(       "\r\n", 
    }       
}
以下是生成静态页的练习:
   
///
public
}
以下是生成静态页的练习:
using Commons.Collections;
 using NVelocity;
 using NVelocity.App;
 using NVelocity.Context;
 using NVelocity.Runtime;
 /// 
///这个测试是基于NVelocity模板引擎实现的.
 /// 
publicNVelocity_模板引擎测试 : System.Web.UI.Page
   {
      protected Page_Load( object sender, EventArgs e)
          {
              //创建NVelocity引擎的实例对象
        VelocityEngine velocity         = VelocityEngine();
               //初始化该实例对象
        ExtendedProperties props         = 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( "myTemplate.html");
        IContext context         = VelocityContext();
        context.Put(        "from", 
        context.Put(       "to", 
        context.Put(       "subject", to nvelocity  ");
        context.Put(       "name", 
              //合并模板
        StringWriter writer         = StringWriter();
               //velocity.MergeTemplate(context, writer); 
        temp.Merge(context, writer);
                      //生成静态页
 (StreamWriter writer2   = StreamWriter(Server.MapPath( "/") Encoding.UTF8,  200))
               {
            writer2.Write(writer);
            writer2.Flush();
            writer2.Close();
        }                                        
    }       
}   
///
public
}
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号