Take the community feedback survey now.

sunylcumar
May 18, 2025
  439
(3 votes)

Render ContentArea without wrapping them in surrounding div

CustomContentAreaRenderer is a specialized class that overrides the default ContentAreaRenderer. It customizes the rendering behavior for content area items, specifically by rendering content items without wrapping them in a surrounding <div> element, which is typically used in the default implementation.

public class CustomContentAreaRenderer : ContentAreaRenderer
{
    protected override void RenderContentAreaItem(
    IHtmlHelper htmlHelper,
    ContentAreaItem contentAreaItem,
    string templateTag,
    string htmlTag,
    string cssClass)
    {
        if (contentAreaItem == null)
        {
            return;
        }

        var content = contentAreaItem.LoadContent();
        if (content == null)
        {
            return;
        }

        // Directly render the content item without surrounding div
        htmlHelper.RenderContentData(content, false);
    }
}
May 18, 2025

Comments

Mark Stott
Mark Stott May 19, 2025 08:31 AM

Hello Sunil,

Thank you for sharing, as an alternative option you can also create a HTML Helper method like so, this removes the div that wraps the whole content area and changes the div that wraps the block to be a section element.:

public static IHtmlContent RenderContentAreaFor<T>(this IHtmlHelper<T> htmlHelper, Expression<Func<T, ContentArea?>> contentAreaFunction)
{
    return htmlHelper.PropertyFor(contentAreaFunction, new
    {
        HasContainer = false,
        ChildrenCustomTagName = "section"
    });
}

I've seen similar approaches to your own where additional common UI logic is applied to the containing element for the block so that the interior razor file for the block can focus just on it's own internal content.

Regards
Mark

sunylcumar
sunylcumar May 22, 2025 03:36 PM

Hi Mark,

 

Thanks for the suggestion. I will try incorporating your helper method to my project.

We can use your helper method in the Razor as follows

@model YourNamespace.Models.StandardPage

@Html.RenderContentAreaFor(m => m.MainContentArea)

Please login to comment.
Latest blogs
Meet the newest OMVPs – summer 2025 cohort

We’re excited to welcome the latest group of Optimizely Most Valuable Professionals (OMVPs) into the program! This summer’s cohort highlights a ble...

Satata Satez | Sep 5, 2025

The Sweet Spot: Hybrid Headless Architecture

When it comes to content management architecture, the pendulum often swings between tightly coupled “headed” CMS setups and the flexibility of full...

Minesh Shah (Netcel) | Sep 4, 2025

Preview Unpublished Pages and Blocks on the Frontend (Optimizely CMS 12)

Introduction In my previous post , I explained how to customize the ContentArea rendering pipeline in Optimizely CMS 12 so editors can see...

Adnan Zameer | Sep 4, 2025 |

How to automatically remove orphaned Opti jobs from the DB

Optimizely CMS provides a simple yet powerful built-in job system that handles most standard scheduling scenarios with ease. Developers can easily...

Stanisław Szołkowski | Sep 4, 2025 |