標籤:
因為小弟寫個方法還是很容易的,但是對於前端相關的東西卻不是很懂,在我自己做銅梁視窗這個小站的時候,涉及到每個頻道一個模板,每個頻道的相關頁面也是一個模板,小弟沒有吧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; }
小弟程式不精,請各位關照關照,順便看看小弟的小站有何改進之處---》銅梁視窗
c#寫的一個html模板方案,不知道大家在asp.net上面使用的是不是也是這個原理