Asp.net动态页面静态化之include和parse区别

来源:互联网 发布:淘宝宝贝排名软件 编辑:程序博客网 时间:2024/05/21 21:34

Asp.net动态页面静态化之include和parse区别

    #include就是在模版中在将其他模版包括进来,就好比网站的头部,尾部,广告模版等等,这些内容都是相同的时候,就可以做成一个单独的模版供各处引用。

  #parse的用法跟#include相类似,如果将上面的代码改成#parse之后,效果是一样的,#parse的特殊功能在于,它可以解析Nvelocity元素,比如,body.html 模版使用Nvelocity变量  $body ,如果使用#parse引用head.html和footer.html两个模版,则在head.html、footer.html模版中继续可以使用$body这个变量,而#include做不到,并且相关的Nvelocity元素(#foreach、#if)也不起效果,只能原样输出,所以#parse > #inclued。

 

c#代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using NVelocity;using NVelocity.App;using NVelocity.Runtime;namespace czbk{    /// <summary>    /// Test 的摘要说明    /// </summary>    public class Test : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            string show = "我是测试include 和parse的区别的";            context.Response.ContentType = "text/html";            VelocityEngine vltEngine = new VelocityEngine();            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/"));//模板文件所在的文件夹            vltEngine.Init();            VelocityContext vltContext = new VelocityContext();            vltContext.Put("show", show);            Template vltTemplate = vltEngine.GetTemplate("self.htm");            System.IO.StringWriter vltWriter = new System.IO.StringWriter();            vltTemplate.Merge(vltContext, vltWriter);            string html = vltWriter.GetStringBuilder().ToString();            context.Response.Write(html);        }        public bool IsReusable        {            get            {                return false;            }        }    }}

other公共html网页

<span style="color:Red">我是通过include过来的,也可以通过parse过来</span><span style="color:Red">我是测试:$show</span>


 

使用include的self.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>#include("others.htm")<br />=========华丽的分割线==============<br /></body></html>


效果如图:

 

使用parse的self.hrml页面

<!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>#parse("others.htm")<br />=========华丽的分割线==============<br /></body></html>


 

效果如图

 


 

0 0
原创粉丝点击