/// <summary>
        /// 返回渲染后的模板文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public void GenerateHomeHtml()
        {
            try
            {
                var templateModel = TemplateManagerCache.GetHomeTemplate();
                if (templateModel.id == 0)
                {
                    throw new Exception("找不到模板");
                }
                //加载模板
                //this.LoadTemplate(templateModel.template_content);
                string templateFile = Path.Combine(GlobalParamsDto.WebRoot, templateModel.template_file);
                this.Document = new TemplateDocument(templateModel.template_content, GlobalParamsDto.WebRoot, templateFile);
                this.Document.Variables.SetValue("this", this);
                //设置顶部导航条数据
                var navigations = _generateContentApp.GetChannelTree();
                this.Document.Variables.SetValue("navigations", navigations);

                //获取栏目文章模板
                ElementCollection <Template> templates = this.Document.GetChildTemplatesByName("channels");
                foreach (Template template in templates)
                {
                    string total = template.Attributes.GetValue("total", "10");
                    //根据模板块里定义的type属性条件取得新闻数据
                    var data = _generateContentApp.GetContentSummary(template.Attributes.GetValue("type"), 1, int.Parse(total));
                    //设置变量newsdata的值
                    template.Variables.SetValue("contents", data);

                    //取得模板块下Id为newslist的标签(也即是在cnblogs_newsdata.html文件中定义的foreach标签)
                    //Tag tag = template.GetChildTagById("newslist");
                    //if (tag is ForEachTag)
                    //{
                    //    //如果标签为foreach标签则设置其BeforeRender事件用于设置变量表达式{$:#.news.url}的值
                    //    tag.BeforeRender += new System.ComponentModel.CancelEventHandler(Tag_BeforeRender);
                    //}
                }

                string contentFilePath = Path.Combine(GlobalParamsDto.WebRoot, "index.html");
                using (var filestream = new FileStream(contentFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    string renderHtml = this.Document.GetRenderText();

                    using (StreamWriter writer = new StreamWriter(filestream, Encoding.UTF8))
                    {
                        writer.WriteLine(renderHtml);
                        writer.Flush();
                    }
                }
            }
            catch (Exception ex)
            {
                LogNHelper.Exception(ex);
            }
        }
        /// <summary>
        /// 返回渲染后的模板文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public (bool genStatus, string contentHtml) GenerateChannelHtml(int channelId)
        {
            try
            {
                var channel = _generateContentApp.GetChannel(channelId);
                if (channel == null)
                {
                    return(false, null);
                }
                var templateModel = TemplateManagerCache.GetChannelTemplate(channel.channel_template);
                if (templateModel == null)
                {
                    return(false, null);
                }
                //加载模板
                //this.LoadTemplate(templateModel.template_content);"Template/Channel"
                string templateFile = Path.Combine(GlobalParamsDto.WebRoot, "Template/Channel", templateModel.template_file);
                this.Document = new TemplateDocument(templateModel.template_content, GlobalParamsDto.WebRoot, templateFile);
                this.Document.Variables.SetValue("this", this);
                //设置顶部导航条数据
                var navigations = _generateContentApp.GetChannelTree();
                this.Document.Variables.SetValue("navigations", navigations);

                //解析文章列表模板设置数据源
                Tag    element = this.Document.GetChildTagById("contents");
                string total   = element.Attributes.GetValue("total", "8");

                var contents = _generateContentApp.GetContentSummary(channelId, 1, int.Parse(total));
                //设置变量newsdata的值
                this.Document.Variables.SetValue("channel", channel);
                this.Document.Variables.SetValue("contents", contents);

                string renderHtml = this.Document.GetRenderText();
                return(true, renderHtml);
            }
            catch (Exception ex)
            {
                LogNHelper.Exception(ex);
            }
            return(false, null);
        }
Exemple #3
0
        /// <summary>
        /// 返回渲染后的模板文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public (bool genStatus, string contentHtml) GenerateChannelHtml(int channelId, int page)
        {
            try
            {
                //Stopwatch watcher = new Stopwatch();
                //watcher.Start();
                var channel = _generateContentApp.GetChannel(channelId);
                if (channel == null)
                {
                    return(false, null);
                }
                var templateModel = TemplateManagerCache.GetChannelTemplate(channel.channel_template);
                if (templateModel == null)
                {
                    return(false, null);
                }

                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                //this.LoadTemplate(templateModel.template_content);"Template/Channel"
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalParamsDto.WebRoot, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalParamsDto.WebRoot, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }

                this.Document.Variables.SetValue("this", this);
                //站点数据
                var site = SiteManagerCache.GetSiteInfo();
                site.site_title = channel.channel_name;
                this.Document.Variables.SetValue("site", site);
                //设置顶部导航条数据
                var navigations = _generateContentApp.GetChannelTree(channelId);
                this.Document.Variables.SetValue("navigations", navigations);

                //解析文章列表模板设置数据源
                Tag    element = this.Document.GetChildTagById("contents");
                string total   = element.Attributes.GetValue("total", "8");

                var contents = _generateContentApp.GetContentSummaryPage(channelId, page, int.Parse(total));
                //设置变量newsdata的值
                this.Document.Variables.SetValue("channel", channel);
                this.Document.Variables.SetValue("contents", contents.Contents);
                this.Document.Variables.SetValue("pageHtml", contents.PageHtml);
                string renderHtml = this.Document.GetRenderText();


                //watcher.Stop();
                //string msg = $"渲染栏目耗时:{watcher.ElapsedMilliseconds} ms";

                //LogNHelper.Info(msg);

                return(true, renderHtml);
            }
            catch (Exception ex)
            {
                LogNHelper.Exception(ex);
            }
            return(false, null);
        }