当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net动态页面静态化之include和parse区别

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

2019年05月10日  | 移动技术网IT编程  | 我要评论

张雨菲冒充本兮,熊猫祖玛,白发老头背影家园

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。

 

代码

using system;
using system.collections.generic;
using system.linq;
using system.web;
using nvelocity;
using nvelocity.app;
using nvelocity.runtime;

namespace czbk
{
    /// 
    /// test 的摘要说明
    /// 
    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网页

我是通过include过来的,也可以通过parse过来
我是测试:$show

 

使用include的self.html页面


,张雨菲冒充本兮,熊猫祖玛,白发老头背影家园
#include(others.htm)
=========华丽的分割线==============


效果如图:

\

 

使用parse的self.hrml页面


,张雨菲冒充本兮,熊猫祖玛,白发老头背影家园
#parse(others.htm)
=========华丽的分割线==============

 

效果如图

\

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网