本文介绍了获取占位符Sitecore中的组件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个组件需要知道当前添加到同一个占位符的组件数量,这是因为它需要根据占位符中的索引更新html属性的值。
I have a component that needs to know how many components thats currently added to the very same placeholder, this since it needs to update a value of an html-attribute based on its index within the placeholder.
无论如何要获得已添加到占位符的组件数量,还是获取当前渲染器索引?
Is there anyway to get either the number of components thats already added to a placeholder, or to get the current-renderers index?
通常我会使用一个简单的for循环并设置属性,但因为它的占位符的组件不是一个选项。
Usually I would just use a simple for-loop and set the attribute, but since its a placeholder with components thats not an option.
提前致谢!
推荐答案
试试这个:
var placeholder = "my-placeholder";
var renderingReferences = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
var renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.EndsWith('/' + placeholder, StringComparison.OrdinalIgnoreCase));
var numberOfRenderingsInPlaceholder = renderingsInPlaceholder.Count();
更新:从 IndexOf
到 EndsWith
。
这篇关于获取占位符Sitecore中的组件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!