protected override void RenderContentAreaItem(
            HtmlHelper htmlHelper,
            ContentAreaItem contentAreaItem,
            string templateTag,
            string htmlTag,
            string cssClass)
        {
            var renderSettings = new Dictionary <string, object>
            {
                ["childrencustomtagname"] = htmlTag,
                ["childrencssclass"]      = cssClass,
                ["tag"] = templateTag
            };

            renderSettings = contentAreaItem.RenderSettings.Concat(
                from r in renderSettings
                where !contentAreaItem.RenderSettings.ContainsKey(r.Key)
                select r).ToDictionary(r => r.Key, r => r.Value);

            htmlHelper.ViewBag.RenderSettings = renderSettings;
            var content = _contentAreaLoader.Get(contentAreaItem);

            if (content == null)
            {
                return;
            }

            bool isInEditMode = IsInEditMode(htmlHelper);

            using (new ContentAreaContext(htmlHelper.ViewContext.RequestContext, content.ContentLink))
            {
                var templateModel = ResolveTemplate(htmlHelper, content, templateTag);
                if (templateModel != null || isInEditMode)
                {
                    bool renderWrappingElement = ShouldRenderWrappingElementForContentAreaItem(htmlHelper);

                    // The code for this method has been c/p from EPiServer.Web.Mvc.Html.ContentAreaRenderer.
                    // The only difference is this if/else block.
                    if (isInEditMode || renderWrappingElement)
                    {
                        var tagBuilder = new TagBuilder(htmlTag);
                        AddNonEmptyCssClass(tagBuilder, cssClass);
                        tagBuilder.MergeAttributes(_attributeAssembler.GetAttributes(
                                                       contentAreaItem,
                                                       isInEditMode,
                                                       templateModel != null));
                        BeforeRenderContentAreaItemStartTag(tagBuilder, contentAreaItem);
                        htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
                        htmlHelper.RenderContentData(content, true, templateModel, _contentRenderer);
                        htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.EndTag));
                    }
                    else
                    {
                        // This is the extra code: don't render wrapping elements in view mode
                        htmlHelper.RenderContentData(content, true, templateModel, _contentRenderer);
                    }
                }
            }
        }
Esempio n. 2
0
        protected override void RenderContentAreaItem(HtmlHelper htmlHelper, ContentAreaItem contentAreaItem, string templateTag, string htmlTag, string cssClass)
        {
            var dictionary = new Dictionary <string, object>();

            dictionary["childrencustomtagname"] = htmlTag;
            dictionary["childrencssclass"]      = cssClass;
            dictionary["tag"] = templateTag;

            dictionary = contentAreaItem.RenderSettings.Concat(
                (
                    from r in dictionary
                    where !contentAreaItem.RenderSettings.ContainsKey(r.Key)
                    select r
                )
                ).ToDictionary(r => r.Key, r => r.Value);

            htmlHelper.ViewBag.RenderSettings = dictionary;
            var content = contentAreaItem.GetContent();

            if (content != null)
            {
                try
                {
                    using (new ContentAreaContext(htmlHelper.ViewContext.RequestContext, content.ContentLink))
                    {
                        var templateModel = ResolveTemplate(htmlHelper, content, templateTag);
                        if ((templateModel != null) || IsInEditMode(htmlHelper))
                        {
                            if (IsInEditMode(htmlHelper))
                            {
                                var tagBuilder = new TagBuilder(htmlTag);
                                AddNonEmptyCssClass(tagBuilder, cssClass);
                                tagBuilder.MergeAttributes <string, string>(
                                    _attributeAssembler.GetAttributes(
                                        contentAreaItem, IsInEditMode(htmlHelper), (bool)(templateModel != null)));
                                BeforeRenderContentAreaItemStartTag(tagBuilder, contentAreaItem);
                                htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
                                htmlHelper.RenderContentData(content, true, templateModel, _contentRenderer);
                                htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.EndTag));
                            }
                            else
                            {
                                htmlHelper.RenderContentData(content, true, templateModel, _contentRenderer);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.Error("[CustomContentAreaRenderer.RenderContentAreaItem] exception", e);

                    if (PageEditing.PageIsInEditMode)
                    {
                        var errorModel = new ContentRenderingErrorModel(content, e);
                        htmlHelper.RenderPartial("TemplateError", errorModel);
                    }
                }
            }
        }