在建站过程中,我们经常要根据父栏目调用所有子栏目下的文章,iNethinkCMS数据库建立的有所有子栏目,但是没有对应的标签去解析。所以,我添加了一个标签。[sys:GetChildIDs(1)]
WebUI_Template.cs中增加代码:
/// <summary> /// 解析指定栏目下的ChildIDs /// </summary> /// <returns></returns> public string Parser_GetChildIDs() { iNethinkCMS.Model.Model_iNethinkCMS_Channel channel = new Model.Model_iNethinkCMS_Channel(); iNethinkCMS.BLL.BLL_iNethinkCMS_Channel channelBll = new BLL.BLL_iNethinkCMS_Channel(); string ChildIDs = "0"; Regex regex = new Regex(@"\[sys:GetChildIDs\((\d+)\)\]", RegexOptions.IgnoreCase); MatchCollection matchCollection = regex.Matches(vContent); foreach (Match m in matchCollection) { int Cid = int.Parse(m.Groups[1].Value); if (Cid<1) { Cid = 1; } channel = channelBll.GetModel(Cid); ChildIDs = channel.ChildIDs; vContent = vContent.Replace(m.Value, ChildIDs); } return vContent; }
在需要添加获取所有子栏目ID的后台页面增加代码如下,以首页为例在Fun_GetIndexContent方法中增加 wt.Parser_GetChildIDs();:
private string Fun_GetIndexContent(int byPage) { string vTemplateUrl = (siteConfig.IndexTemplateName); WebUI_Template wt = new WebUI_Template(); wt.Load_Template(vTemplateUrl); wt.vPage = byPage; wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase); wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase); wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:keywords}"), seoConfig.IndexKeywords, RegexOptions.IgnoreCase); wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase); wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitepath}"), "<a href=\"/\">首页</a>", RegexOptions.IgnoreCase); wt.Parser_GetChildIDs();//此处增加的解析获取所有子栏目ID的标签 wt.Parser_MyTag(); wt.Parser_List(); wt.Parser_Page(); wt.Parser_IF(); return wt.vContent; }
首页index.html模板中读取父栏目ID为1的所有子栏目的文章示例:
<!--list:{$Sql=Select Top 10 * From iNethinkCMS_Content Where [Display]=1 and Cid in([sys:GetChildIDs(1)]) Order by Id Desc }-->