Do not render a Section when all widgets return empty content

Sander Geerts asked on August 9, 2022 15:22

Is it possible to know in the live site in a Section if any of the widgets it contains will render anything?

We have some sections that contain spacing like this:

<div class="section" style="margin-bottom: 20px; background-color: #f4f4f4;">

But sometimes we have widgets that display something based on the presence of certain content. If that is the case, we don't want to display neither the widget AND the section it is in.

Example widget:

public class SimpleWidgetController : WidgetController
    public ActionResult Index()
        var content = GetContent();
        if (!content.Any())
            return null;

        return View("Widgets/_Content", content);

So in the case where GetContent() does not return any results, we don't want to render the widget and also not the section it is in.

We cannot capture the result of Html.Kentico().WidgetZone(), because that method will always return null in the live site and renders directly to the output through MVC's System.Web.Mvc.Html.ChildActionExtensions.RenderAction.

Is this possible in Kentico?

   Please, sign in to be able to submit a new answer.