/// <summary> /// 获得模板字符串,从设置中的模板路径来读取模板文件. /// </summary> /// <param name="sitePath">站点目录</param> /// <param name="tempPath">模板目录</param> /// <param name="skinName">模板名</param> /// <param name="templateName">模板文件的文件名称</param> /// <param name="fromPage">源页面名称</param> /// <param name="inherit">该页面继承的类</param> /// <param name="buildPath">生成目录名</param> /// <param name="channelName">频道名称</param> /// <param name="nest">嵌套次数</param> /// <returns>string值,如果失败则为"",成功则为模板内容的string</returns> public static string GetTemplate(string sitePath, string tempPath, string skinName, string templet, string fromPage, string inherit, string buildPath, string channelName, string pageSize, string isCache, string cachePath, string _type, int _site_id, int nest) { StringBuilder strReturn = new StringBuilder(220000); //返回的字符 string templetFullPath = Utils.GetMapPath(string.Format("{0}{1}/{2}/{3}", sitePath, tempPath, skinName, templet)); //取得模板文件物理路径 //超过5次嵌套退出 if (nest < 1) { nest = 1; } else if (nest > 5) { return(""); } //检查模板文件是否存在 if (!File.Exists(templetFullPath)) { return(""); } //开始读写文件 using (StreamReader objReader = new StreamReader(templetFullPath, Encoding.UTF8)) { string method = string.Empty; //方法 StringBuilder extNamespace = new StringBuilder(); //命名空间标签转换容器 StringBuilder textOutput = new StringBuilder(70000); //格式化模板 string templetContent = objReader.ReadToEnd(); templetContent = Regex.Replace(templetContent, " {2,}", " "); templetContent = Regex.Replace(templetContent, @"\t+", ""); templetContent = Regex.Replace(templetContent, @"[\r\n\s?\r\n]{2,}", "\r\n"); //End textOutput.Append(templetContent); objReader.Close(); //替换Csharp标签 foreach (Match m in r[3].Matches(textOutput.ToString())) { textOutput.Replace(m.Groups[0].ToString(), m.Groups[0].ToString().Replace("\r\n", "\r\t\r")); } //替换命名空间标签 foreach (Match m in r[2].Matches(textOutput.ToString())) { extNamespace.Append("\r\n<%@ Import namespace=\"" + m.Groups[1] + "\" %>"); textOutput.Replace(m.Groups[0].ToString(), string.Empty); } //替换特殊标记 textOutput.Replace("\r\n", "\r\r\r"); textOutput.Replace("<%", "\r\r\n<%"); textOutput.Replace("%>", "%>\r\r\n"); textOutput.Replace("<%csharp%>\r\r\n", "<%csharp%>").Replace("\r\r\n<%/csharp%>", "<%/csharp%>"); //开始查找替换标签 string[] strlist = Utils.SplitString(textOutput.ToString(), "\r\r\n"); for (int i = 0; i < strlist.Length; i++) { if (strlist[i] == "") { continue; } strReturn.Append(ConvertTags(nest, channelName, pageSize, isCache, cachePath, sitePath, tempPath, skinName, strlist[i], _type, _site_id)); //搜索替换标签 } //如果是第一层则写入文件 if (nest == 1) { //定义页面常量 StringBuilder constStr = new StringBuilder(); //网站ID if (_site_id > 0) { constStr.Append("private const int site_id = " + _site_id + ";\r\n"); } //频道名称 if (channelName != string.Empty) { constStr.Append("private const string channel = \"" + channelName + "\";\r\n"); if ("index" == _type) //频道首页 { method = "\tset_channel_model(channel);\r\n"; } else if ("list" == _type || "detail" == _type || "category" == _type) //频道列表、详情 { method = "\tvalidate_channel_data(channel);\r\n"; } } //分页 if (pageSize != string.Empty && Utils.StrToInt(pageSize, 0) > 0) { constStr.Append("private const int pagesize = " + pageSize + ";\r\n"); } //文件内容 string pagePath = string.Format("{0}aspx/{1}/", sitePath, buildPath); //目录路径 string pageDir = Utils.GetMapPath(pagePath); //生成文件的目录路径 string outputPath = pageDir + fromPage; //生成文件的物理路径 //如果物理路径不存在则创建 if (!Directory.Exists(pageDir)) { Directory.CreateDirectory(pageDir); } //2016-10-28改造 StringBuilder cs = new StringBuilder(); //是否开启页面缓存 if (Utils.StrToInt(isCache, 0) != 0) { cs.AppendFormat("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n", inherit); cs.Append("<%@ Import namespace=\"System.Collections.Generic\" %>\r\n"); cs.Append("<%@ Import namespace=\"System.Text\" %>\r\n"); cs.Append("<%@ Import namespace=\"System.Data\" %>\r\n"); cs.Append("<%@ Import namespace=\"DTcms.Common\" %>\r\n"); cs.Append("<script runat=\"server\">\r\n"); cs.Append(constStr); cs.Append("override protected void OnInit(EventArgs e)\r\n"); cs.Append("{\r\n"); cs.Append("\tbase.OnInit(e);\r\n"); cs.AppendFormat("\tstring cacheKey = config.cachekey + \"_{0}_{1}\";\r\n", buildPath, PageCache.CachePathFormart(cachePath)); cs.Append("\tstring outHtml = null;\r\n"); cs.Append(method); cs.Append("\tif (config.systemcache == 1){\r\n"); cs.Append("\t\toutHtml = PageCache.GetCache(cacheKey);\r\n"); cs.Append("\t}\r\n"); cs.Append("\tif(null == outHtml){\r\n"); cs.AppendFormat("\t\tStringBuilder templateBuilder = new StringBuilder({0});\r\n", strReturn.Capacity); cs.Append("\t\t"); cs.Append(Regex.Replace(strReturn.ToString(), @"\r\n\s*templateBuilder\.Append\(""""\);(\r\n)?", "")); cs.Append("\r\n"); cs.Append("\t\toutHtml = templateBuilder.ToString();\r\n"); cs.Append("\t\tPageCache.WriteCache(outHtml, config.systemcache, config.cachetime, cacheKey, config.fomatpage);\r\n"); cs.Append("\t}\r\n"); cs.Append("\tResponse.Write(outHtml);\r\n"); cs.Append("}\r\n"); cs.Append("</script>"); } else { cs.AppendFormat("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n", inherit); cs.Append("<%@ Import namespace=\"System.Collections.Generic\" %>\r\n"); cs.Append("<%@ Import namespace=\"System.Text\" %>\r\n"); cs.Append("<%@ Import namespace=\"System.Data\" %>\r\n"); cs.Append("<%@ Import namespace=\"DTcms.Common\" %>\r\n"); cs.Append("<script runat=\"server\">\r\n"); cs.Append(constStr); cs.Append("override protected void OnInit(EventArgs e)\r\n"); cs.Append("{\r\n"); cs.Append("\tbase.OnInit(e);\r\n"); cs.Append(method); cs.AppendFormat("\tStringBuilder templateBuilder = new StringBuilder({0});\r\n", strReturn.Capacity); cs.Append("\t"); cs.Append(Regex.Replace(strReturn.ToString(), @"\r\n\s*templateBuilder\.Append\(""""\);(\r\n)?", "")); cs.Append("\r\n"); cs.Append("\tResponse.Write(templateBuilder.ToString());\r\n"); cs.Append("}\r\n"); cs.Append("</script>"); } //保存写入文件 File.WriteAllText(outputPath, cs.ToString(), Encoding.UTF8); } } return(strReturn.ToString()); }
/// <summary> /// 获得模板字符串,从设置中的模板路径来读取模板文件. /// </summary> /// <param name="sitePath">站点目录</param> /// <param name="tempPath">模板目录</param> /// <param name="skinName">模板名</param> /// <param name="templateName">模板文件的文件名称</param> /// <param name="fromPage">源页面名称</param> /// <param name="inherit">该页面继承的类</param> /// <param name="buildPath">生成目录名</param> /// <param name="channelName">频道名称</param> /// <param name="nest">嵌套次数</param> /// <returns>string值,如果失败则为"",成功则为模板内容的string</returns> public static string GetTemplate(string sitePath, string tempPath, string skinName, string templet, string fromPage, string inherit, string buildPath, string channelName, string pageSize, string isCache, string cachePath, string cacheFix, string _type, int _site_id, int nest) { StringBuilder strReturn = new StringBuilder(220000); //返回的字符 string templetFullPath = Utils.GetMapPath(string.Format("{0}{1}/{2}/{3}", sitePath, tempPath, skinName, templet)); //取得模板文件物理路径 //超过5次嵌套退出 if (nest < 1) { nest = 1; } else if (nest > 5) { return(""); } //检查模板文件是否存在 if (!File.Exists(templetFullPath)) { return(""); } //开始读写文件 using (StreamReader objReader = new StreamReader(templetFullPath, Encoding.UTF8)) { string method = string.Empty; //方法 StringBuilder extNamespace = new StringBuilder(); //命名空间标签转换容器 StringBuilder textOutput = new StringBuilder(70000); textOutput.Append(objReader.ReadToEnd()); objReader.Close(); //替换Csharp标签 foreach (Match m in r[3].Matches(textOutput.ToString())) { textOutput.Replace(m.Groups[0].ToString(), m.Groups[0].ToString().Replace("\r\n", "\r\t\r")); } //替换命名空间标签 foreach (Match m in r[2].Matches(textOutput.ToString())) { extNamespace.Append("\r\n<%@ Import namespace=\"" + m.Groups[1] + "\" %>"); textOutput.Replace(m.Groups[0].ToString(), string.Empty); } //替换特殊标记 textOutput.Replace("\r\n", "\r\r\r"); textOutput.Replace("<%", "\r\r\n<%"); textOutput.Replace("%>", "%>\r\r\n"); textOutput.Replace("<%csharp%>\r\r\n", "<%csharp%>").Replace("\r\r\n<%/csharp%>", "<%/csharp%>"); //开始查找替换标签 string[] strlist = Utils.SplitString(textOutput.ToString(), "\r\r\n"); for (int i = 0; i < strlist.Length; i++) { if (strlist[i] == "") { continue; } strReturn.Append(ConvertTags(nest, channelName, pageSize, isCache, cachePath, sitePath, tempPath, skinName, strlist[i], cacheFix, _type, _site_id)); //搜索替换标签 } //如果是第一层则写入文件 if (nest == 1) { //定义页面常量 StringBuilder constStr = new StringBuilder(); //网站ID if (_site_id > 0) { constStr.Append("private const int site_id = " + _site_id + ";\r\n"); } //频道名称 if (channelName != string.Empty) { constStr.Append("private const string channel = \"" + channelName + "\";\r\n"); if ("index" == _type) //频道首页 { method = "\r\n\tset_channel_model(channel);"; } else if ("list" == _type || "detail" == _type || "category" == _type) //频道列表、详情 { method = "\r\n\tvalidate_channel_data(channel);"; } } //分页 if (pageSize != string.Empty && Utils.StrToInt(pageSize, 0) > 0) { constStr.Append("private const int pagesize = " + pageSize + ";\r\n"); } //文件内容 string template = null; string pagePath = string.Format("{0}aspx/{1}/", sitePath, buildPath); //目录路径 string pageDir = Utils.GetMapPath(pagePath); //生成文件的目录路径 string outputPath = pageDir + fromPage; //生成文件的物理路径 //如果物理路径不存在则创建 if (!Directory.Exists(pageDir)) { Directory.CreateDirectory(pageDir); } //是否开启页面缓存 if (Utils.StrToInt(isCache, 0) != 0) { //缓存目录 string cacheDir = pagePath + "cache/"; //判断缓存目录是否存在 Utils.DirectoryExists(cacheDir); template = string.Format("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n" + "<%@ Import namespace=\"System.Collections.Generic\" %>\r\n" + "<%@ Import namespace=\"System.Text\" %>\r\n" + "<%@ Import namespace=\"System.Data\" %>\r\n" + "<%@ Import namespace=\"DTcms.Common\" %>\r\n" + "<script runat=\"server\">\r\nprivate bool writeHtml = false;\r\nprivate bool writeCache = false;\r\n{3}private string cacheFile = null;\r\noverride protected void OnInit(EventArgs e)\r\n" + "{{\r\n\tcacheFile = \"{1}\";" + "{5}\r\n\t" + "if (config.pagecache == 1 && Utils.FileExists(cacheFile)){{\r\n\t\t" + "this.Response.Write(PageCache.ReadHtmlFile(cacheFile));\r\n\t\t" + "if (PageCache.CacheFileTime(cacheFile, config.cachetime)){{\r\n\t\t\tthis.Response.End();\r\n\t\t}}\r\n\t\telse\r\n\t\t{{\r\n\t\t\twriteCache = true;\r\n\t\t}}\r\n\t" + "}}\r\n\telse{{\r\n\t\twriteHtml = true;\r\n\t\twriteCache = true;\r\n\t" + "}}\r\n\tbase.OnInit(e);\r\n}}\r\nprotected override void Render( HtmlTextWriter writer )\r\n{{\r\n\t" + "if (this.writeCache)\r\n\t{{\r\n\t" + "StringBuilder templateBuilder = new StringBuilder({2});\r\n{4}\r\n\t" + "if (this.writeHtml){{\r\n\t\tthis.Response.Write(templateBuilder.ToString());\r\n\t}}\r\n\t" + "if (config.pagecache == 1){{\r\n\t\tPageCache.WriteHtmlFile(config.pagecache, config.fomatpage, cacheFile, templateBuilder.ToString(), config.cachetime);\r\n\t}}\r\n\t" + "this.Response.End();\r\n\t}}\r\n\telse\r\n\t{{\r\n\t\tbase.Render(writer);\r\n\t}}\r\n}}\r\n</script>\r\n", inherit, cacheDir + PageCache.CachePathFormart(cachePath) + "." + cacheFix, strReturn.Capacity, constStr.ToString(), Regex.Replace(strReturn.ToString(), @"\r\n\s*templateBuilder\.Append\(""""\);", ""), method); } else { template = string.Format("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n" + "<%@ Import namespace=\"System.Collections.Generic\" %>\r\n" + "<%@ Import namespace=\"System.Text\" %>\r\n" + "<%@ Import namespace=\"System.Data\" %>\r\n" + "<%@ Import namespace=\"DTcms.Common\" %>\r\n\r\n" + "<script runat=\"server\">\r\n{2}override protected void OnInit(EventArgs e)\r\n" + "{{\r\n\r\n\tbase.OnInit(e);\r\n\t" + "{4}\r\n\t" + "StringBuilder templateBuilder = new StringBuilder({1});{3}\r\n\t" + "Response.Write(templateBuilder.ToString());\r\n}}\r\n</script>\r\n", inherit, strReturn.Capacity, constStr.ToString(), Regex.Replace(strReturn.ToString(), @"\r\n\s*templateBuilder\.Append\(""""\);", ""), method); } //保存写入文件 File.WriteAllText(outputPath, template, Encoding.UTF8); } } return(strReturn.ToString()); }