Sitecore 设置组件数量

Sitecore Set the Number of Components

是否可以设置placeholder中的组件个数?

我们可以在灰色框中使用 "Add to here" 在占位符中添加尽可能多的组件,即使组件已经添加。

我想说

In plcaceholder named 'bodyArea', you can set only one component in 'bodyArea' placeholder and you will not add any other component additionally.

有什么办法吗??

这是一个博客,其中描述了如何限制允许的控件数量:

http://www.newguid.net/sitecore/2014/restricting-the-number-of-components-in-the-sitecore-page-editor/

其他解决方案是使用规则:

http://dotnetmafia.com/blogs/kevin/archive/2013/07/10/placeholder-settings-rules.aspx

可能有很多方法,但这是我以前使用的。

// Check the number of renderings in placeholder
public static bool numberOfRenderings(string placeholderName)
{
    bool rendering = true;
    var renderingReferences = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
    int renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.EndsWith('/' + placeholderName, StringComparison.OrdinalIgnoreCase)).Count();

    if (renderingsInPlaceholder > 1)
    {
        return rendering = false;
    }
    return rendering;
}

在View.cshtml

if (@yourObject.numberOfRenderings("your-placeholder-key")) {
    @Html.Sitecore().Placeholder("your-placeholder-key")
}
else 
{
    @Html.Raw("<div>Only one rendering item is available in this placeholder.</div>")
}