Exemple #1
0
        /// <summary>
        /// Renders the section contents.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="webPage">The web page.</param>
        /// <param name="model">The model.</param>
        public static void RenderSectionContents(this HtmlHelper htmlHelper, WebPageBase webPage, RenderPageViewModel model)
        {
            foreach (var region in model.Regions)
            {
                var contentsBuilder = new StringBuilder();
                var projections     = model.Contents.Where(c => c.RegionId == region.RegionId).OrderBy(c => c.Order).ToList();

                using (new LayoutRegionWrapper(contentsBuilder, region, model.AreRegionsEditable))
                {
                    foreach (var projection in projections)
                    {
                        // Add Html
                        using (new RegionContentWrapper(contentsBuilder, projection, model.CanManageContent && model.AreRegionsEditable))
                        {
                            var content = projection.GetHtml(htmlHelper);
                            contentsBuilder.Append(content);
                        }
                    }
                }

                var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(contentsBuilder, model);
                if (model.AreRegionsEditable)
                {
                    pageHtmlHelper.ReplaceRegionRepresentationHtml();
                }
                var html = pageHtmlHelper.GetReplacedHtml().ToString();

                RenderSectionAsLayoutRegion(webPage, html, region.RegionIdentifier);
            }
        }
Exemple #2
0
        /// <summary>
        /// Renders page to string recursively - going deep to master pages and layouts.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="currentModel">The model.</param>
        /// <param name="pageModel">The page model.</param>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <returns>
        /// String builder with updated data
        /// </returns>
        private static StringBuilder RenderRecursively(CmsControllerBase controller, RenderPageViewModel currentModel, RenderPageViewModel pageModel, HtmlHelper htmlHelper)
        {
            if (currentModel.MasterPage != null)
            {
                var renderedMaster = RenderRecursively(controller, currentModel.MasterPage, pageModel, htmlHelper);

                var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(renderedMaster, pageModel);

                foreach (var region in currentModel.Regions)
                {
                    var contentsBuilder = new StringBuilder();
                    var projections     = currentModel.Contents.Where(c => c.RegionId == region.RegionId).OrderBy(c => c.Order).ToList();

                    using (new LayoutRegionWrapper(contentsBuilder, region, currentModel.AreRegionsEditable))
                    {
                        foreach (var projection in projections)
                        {
                            // Add Html
                            using (new RegionContentWrapper(contentsBuilder, projection, currentModel.CanManageContent && currentModel.AreRegionsEditable))
                            {
                                // Pass current model as view data model
                                htmlHelper.ViewData.Model = pageModel;

                                var content = projection.GetHtml(htmlHelper);
                                contentsBuilder.Append(content);
                            }
                        }
                    }

                    // Insert region to master page
                    var html = contentsBuilder.ToString();
                    pageHtmlHelper.ReplaceRegionHtml(region.RegionIdentifier, html);
                }

                if (currentModel.AreRegionsEditable)
                {
                    pageHtmlHelper.ReplaceRegionRepresentationHtml();
                }
                renderedMaster = pageHtmlHelper.GetReplacedHtml();

                return(renderedMaster);
            }

            var newModel = currentModel.Clone();

            newModel.Id            = pageModel.Id;
            newModel.PageUrl       = pageModel.PageUrl;
            newModel.Title         = pageModel.Title;
            newModel.MetaTitle     = pageModel.MetaTitle;
            newModel.RenderingPage = pageModel;
            newModel.Metadata      = pageModel.Metadata;
            newModel.IsReadOnly    = pageModel.IsReadOnly;

            PopulateCollections(newModel, pageModel);

            var renderedView = RenderViewToString(controller, "~/Areas/bcms-Root/Views/Cms/Index.cshtml", newModel);

            return(new StringBuilder(renderedView));
        }
Exemple #3
0
        /// <summary>
        /// Renders the page custom JavaScript.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="styles">The styles.</param>
        /// <param name="pageModel">The rendering page model.</param>
        /// <returns>The rendering page custom CSS</returns>
        public static IHtmlString RenderPageCustomCss(this HtmlHelper htmlHelper, IEnumerable <IStylesheetAccessor> styles, RenderPageViewModel pageModel = null)
        {
            if (styles != null)
            {
                var includedCssResources = new List <string>();
                var inlineCssBuilder     = new StringBuilder();
                var cssIncludesBuilder   = new StringBuilder();

                foreach (var content in styles)
                {
                    var cssList = content.GetCustomStyles(htmlHelper);
                    if (cssList != null)
                    {
                        foreach (var css in cssList)
                        {
                            if (!string.IsNullOrWhiteSpace(css) && !includedCssResources.Contains(css))
                            {
                                inlineCssBuilder.AppendLine(css);
                                includedCssResources.Add(css);
                            }
                        }
                    }

                    var includes = content.GetStylesResources(htmlHelper);
                    if (includes != null)
                    {
                        foreach (var include in includes)
                        {
                            if (!includedCssResources.Contains(include))
                            {
                                cssIncludesBuilder.AppendLine(string.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />", include));
                                includedCssResources.Add(include);
                            }
                        }
                    }
                }

                if (pageModel != null)
                {
                    var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(inlineCssBuilder, pageModel);
                    inlineCssBuilder = pageHtmlHelper.GetReplacedHtml();
                }

                var inlineCss   = inlineCssBuilder.ToString();
                var includedCss = cssIncludesBuilder.ToString();
                if (!string.IsNullOrWhiteSpace(inlineCss) || !string.IsNullOrWhiteSpace(includedCss))
                {
                    if (!string.IsNullOrWhiteSpace(inlineCss))
                    {
                        inlineCss = string.Format(@"<style type=""text/css"">{0}</style>", inlineCss);
                    }

                    return(new HtmlString(string.Concat(includedCss, inlineCss)));
                }
            }

            return(null);
        }
Exemple #4
0
        /// <summary>
        /// Renders the page custom JavaScript.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="scripts">The scripts.</param>
        /// <param name="pageModel">The renderingpage model.</param>
        /// <returns>Rendering page custom JavaScript</returns>
        public static IHtmlString RenderPageCustomJavaScript(this HtmlHelper htmlHelper, IEnumerable <IJavaScriptAccessor> scripts, RenderPageViewModel pageModel = null)
        {
            if (scripts != null)
            {
                var insertedJsResources = new List <string>();
                var inlineJsBuilder     = new StringBuilder();
                var jsIncludesBuilder   = new StringBuilder();

                foreach (var content in scripts)
                {
                    var jScriptList = content.GetCustomJavaScript(htmlHelper);
                    if (jScriptList != null)
                    {
                        foreach (var jScript in jScriptList)
                        {
                            if (!string.IsNullOrWhiteSpace(jScript) && !insertedJsResources.Contains(jScript))
                            {
                                inlineJsBuilder.Append(@"<script type=""text/javascript"" language=""javascript"">");
                                inlineJsBuilder.Append(jScript);
                                inlineJsBuilder.AppendLine(@"</script>");
                                insertedJsResources.Add(jScript);
                            }
                        }
                    }

                    var includes = content.GetJavaScriptResources(htmlHelper);
                    if (includes != null)
                    {
                        foreach (var include in includes)
                        {
                            if (!insertedJsResources.Contains(include))
                            {
                                jsIncludesBuilder.AppendLine(string.Format(@"<script src=""{0}"" type=""text/javascript""></script>", include));
                                insertedJsResources.Add(include);
                            }
                        }
                    }
                }

                if (pageModel != null)
                {
                    var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(inlineJsBuilder, pageModel);
                    inlineJsBuilder = pageHtmlHelper.GetReplacedHtml();
                }

                var inlineJs   = inlineJsBuilder.ToString();
                var includedJs = jsIncludesBuilder.ToString();
                if (!string.IsNullOrWhiteSpace(inlineJs) || !string.IsNullOrWhiteSpace(includedJs))
                {
                    return(new HtmlString(string.Concat(includedJs, inlineJs)));
                }
            }

            return(null);
        }
        /// <summary>
        /// Renders page to string recursively - going deep to master pages and layouts.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="currentModel">The model.</param>
        /// <param name="pageModel">The page model.</param>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <returns>
        /// String builder with updated data
        /// </returns>
        private static StringBuilder RenderRecursively(CmsControllerBase controller, RenderPageViewModel currentModel, RenderPageViewModel pageModel, HtmlHelper htmlHelper)
        {
            if (currentModel.MasterPage != null)
            {
                var renderedMaster = RenderRecursively(controller, currentModel.MasterPage, pageModel, htmlHelper);

                var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(renderedMaster, pageModel);
                var contentHtmlHelper = new ChildContentRenderHelper(htmlHelper);

                foreach (var region in currentModel.Regions)
                {
                    var contentsBuilder = new StringBuilder();
                    var projections = currentModel.Contents.Where(c => c.RegionId == region.RegionId).OrderBy(c => c.Order).ToList();

                    using (new LayoutRegionWrapper(contentsBuilder, region, currentModel.AreRegionsEditable))
                    {
                        foreach (var projection in projections)
                        {
                            // Add Html
                            using (new RegionContentWrapper(contentsBuilder, projection, currentModel.CanManageContent && currentModel.AreRegionsEditable))
                            {
                                // Pass current model as view data model
                                htmlHelper.ViewData.Model = pageModel;

                                contentsBuilder = contentHtmlHelper.AppendHtml(contentsBuilder, projection);
                            }
                        }
                    }

                    // Insert region to master page
                    var html = contentsBuilder.ToString();
                    pageHtmlHelper.ReplaceRegionHtml(region.RegionIdentifier, html);
                }

                if (currentModel.AreRegionsEditable)
                {
                    pageHtmlHelper.ReplaceRegionRepresentationHtml();
                }
                renderedMaster = pageHtmlHelper.GetReplacedHtml();

                return renderedMaster;
            }

            var newModel = currentModel.Clone();
            newModel.Id = pageModel.Id;
            newModel.PageUrl = pageModel.PageUrl;
            newModel.Title = pageModel.Title;
            newModel.MetaTitle = pageModel.MetaTitle;
            newModel.MetaKeywords = pageModel.MetaKeywords;
            newModel.MetaDescription = pageModel.MetaDescription;
            newModel.RenderingPage = pageModel;
            newModel.Metadata = pageModel.Metadata;
            newModel.IsReadOnly = pageModel.IsReadOnly;
            newModel.CreatedOn = pageModel.CreatedOn;
            newModel.ModifiedOn = pageModel.ModifiedOn;
            newModel.CreatedByUser = pageModel.CreatedByUser;
            newModel.ModifiedByUser = pageModel.ModifiedByUser;

            PopulateCollections(newModel, pageModel);

            var renderedView = RenderViewToString(controller, "~/Areas/bcms-Root/Views/Cms/Index.cshtml", newModel);
            return new StringBuilder(renderedView);
        }