获取占位符中的组件数量,Sitecore [英] Get number of components in placeholder, 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
.
Update: Changed search for placeholder key from IndexOf
to EndsWith
.
这篇关于获取占位符中的组件数量,Sitecore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!