Sitecore-如果占位符为空,则隐藏渲染

Sitecore-如果占位符为空,则隐藏渲染,sitecore,sitecore7,sitecore-mvc,Sitecore,Sitecore7,Sitecore Mvc,我有一个相当简单的Sitecore MVC呈现,其中包含一个标题字段和一个占位符: <section> <div class="container"> <h2 class="m-header"><span>@Html.Sitecore().Field("PromoItemsHeader")</span></h2> <div class="l-section grid">

我有一个相当简单的Sitecore MVC呈现,其中包含一个标题字段和一个占位符:

<section>
    <div class="container">
        <h2 class="m-header"><span>@Html.Sitecore().Field("PromoItemsHeader")</span></h2>

        <div class="l-section grid">
            @Html.Sitecore().Placeholder("PromoItems")
        </div>
    </div>
</section>

@Html.Sitecore()字段(“PromotItemsHeader”)
@Html.Sitecore()占位符(“PromotItems”)
仅当占位符包含项目时,我希望此呈现在页面编辑模式之外显示。这似乎应该很简单,但我找不到一个明显的/干净的方法。试试这个:

Sitecore.Context.Page.Renderings
    .Count(r => r.Placeholder.IndexOf("PromoItems", StringComparison.OrdinalIgnoreCase) > -1)
如果希望仅对数据源进行渲染,可以添加以下内容:

Sitecore.Context.Page.Renderings
    .Where(r => r.Placeholder.IndexOf("PromoItems", StringComparison.OrdinalIgnoreCase) > -1)
    .Count(r => !string.IsNullOrWhiteSpace(r.Settings.DataSource))

我现在只需向视图模型添加一个新属性,如果占位符包含任何渲染,该属性将返回。

Sitecore.Context.Page.renderings
是我在以前的Sitecore 7.x webforms项目中使用的,但在Sitecore 8.x MVC+Glass land中,它似乎不适用于我——在我的布局视图中,渲染集合总是空的。不确定这是8.x版的,还是玻璃版的,还是“我把我的项目搞砸了”之类的

我们所做的工作是:

var pageContext = Sitecore.Mvc.Presentation.PageContext.Current;
var pageDefinition = pageContext.PageDefinition;
bool showSidebar = pageDefinition.Renderings.Any(x => x.Placeholder == "left");