c#写的一个html模板方案,不知道大家在asp.net上面使用的是不是也是这个原理

因为小弟写个方法还是很容易的,但是对于前端相关的东西却不是很懂,在我自己做铜梁视窗这个小站的时候,涉及到每个频道一个模板,每个频道的相关页面也是一个模板,小弟没有吧html写在aspx上面,然后在匹配模板的时候发现很多有些模板是可以公用的,例如首页的head部分,还有其他的频道head部分,还有footer部分,所以写了下面这么一个东西来控制每个页面的模板调用,不知道对不对哈。请各位大牛小牛赐教!

对了,在运营过程中,我服务器每个月会产生1%的磁盘碎片,不知道是否跟这个模板方案有关

 /// <summary>
        /// dir 是template目录下的分文件夹,shop,m,以及其他
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="tpl"></param>
        /// <returns></returns>
        public static string GetPageContent(string dir, string tpl)
        {

            string key = dir + "_" + tpl + "_CONTENT";
            string contentkey = dir + "_CONTENT_CONTENT";

            string temp = "";
            if (DataCache.GetCache(key) == null)
            {
                string filepath = "/Template/" + dir + (dir.Length > 0 ? "/" : "") + tpl + ".html";

                temp = File.ReadAllText(HttpContext.Current.Server.MapPath(filepath), Encoding.UTF8);

                DataCache.SetCache(key, temp);
            }
            else
            {
                temp = DataCache.GetCache(key).ToString();
            }

            string content = "";

            if (DataCache.GetCache(contentkey) == null)
            {
                content = File.ReadAllText(HttpContext.Current.Server.MapPath("/Template/" + dir + "/Content.html"), Encoding.UTF8);
                DataCache.SetCache(contentkey, content);
            }
            else
            {
                content = DataCache.GetCache(contentkey).ToString();
            }

            string html = content.Replace("<$inner$>", temp);

            string headstr = (string)DataCache.GetCache("HEADCONTENT");
            string footstr = (string)DataCache.GetCache("FOOTCONTENT");
            if (headstr == null || footstr == null)
            {
                headstr = File.ReadAllText(HttpContext.Current.Server.MapPath("/Template/head.html"), Encoding.UTF8);
                footstr = File.ReadAllText(HttpContext.Current.Server.MapPath("/Template/foot.html"), Encoding.UTF8);
                DataCache.SetCache("HEADCONTENT", headstr);
                DataCache.SetCache("FOOTCONTENT", footstr);
            }
            
            html = html.Replace("<$footcontent$>", footstr);
            html = html.Replace("<$headcontent$>", headstr);

            if (!string.IsNullOrEmpty(dir))
            {
                CMS_SEO seo = GetCurrectSeo(dir);

                html = html.Replace("<$seoname$>", seo.SiteName);
                html = html.Replace("<$seourl$>", seo.SiteUrl);
                html = html.Replace("<$seoaddress$>", seo.Address);
                html = html.Replace("<$seocopyright$>", seo.Copyright);
                html = html.Replace("<$seodescription$>", seo.Description);
                html = html.Replace("<$seoemail$>", seo.Email);
                html = html.Replace("<$seoicp$>", seo.ICP);
                html = html.Replace("<$seokeywords$>", seo.Keywords);
                html = html.Replace("<$seologo$>", seo.Logo);
                html = html.Replace("<$seotel$>", seo.Tel);
                html = html.Replace("<$seoweixin$>", seo.Weixin);
                html = html.Replace("<$seoworktime$>", seo.WorkTime);
                html = html.Replace("<$OnlineChart$>", seo.OnlineChart);
            }


            html = html.Replace("<$token$>", DateTime.Now.AddHours(1).Ticks + "");


            //广告位            
            html = html.Replace("<$DetailBottom$>", GetAdScript("DetailBottom"));
            html = html.Replace("<$RightContent$>", GetAdScript("RightContent"));
            html = html.Replace("<$BottomAd$>", GetAdScript("BottomAd"));
            //广告位结束


            html = new BLL_Adv().GetPlaceTag(html, dir);

            return html;
        }

  小弟程序不精,请各位关照关照,顺便看看小弟的小站有何改进之处---》铜梁视窗

posted @ 2015-10-01 16:39  板砖博客  阅读(562)  评论(0编辑  收藏  举报