Quantcast
Channel: Joel Abrahamsson
Viewing all articles
Browse latest Browse all 78

RenderContentData with support for rendering tag

$
0
0

EPiServer 7 CMS' API features two HTML helper extensions for rendering partial content with MVC, both named RenderContentData. None of them offer a way to render content using a rendering tag.

The below extension adds such a method:

using System.Web.Mvc;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.Web;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using EPiServer.Web.Mvc;
using EPiServer.Web.Mvc.Html;

namespace MySite
{
    public static class HtmlHelpers
    {
        public static void RenderContentData(
            this HtmlHelper html, 
            IContentData content,
            string tag,
            isInContentArea = false)
        {
            var templateResolver = ServiceLocator.Current.GetInstance<TemplateResolver>();
            var templateModel = templateResolver.Resolve(
                html.ViewContext.HttpContext,
                content.GetOriginalType(),
                content,
                TemplateTypeCategories.MvcPartial,
                tag);

            var contentRenderer = ServiceLocator.Current.GetInstance<IContentRenderer>();
            html.RenderContentData(
                content,
                isInContentArea,
                templateModel,
                contentRenderer);
        }
    }
}

Viewing all articles
Browse latest Browse all 78

Trending Articles