How to calculate/target usercontrol current position on placeholder?

144 Views Asked by At

For a project I'm using Sitecore CMS. The HMTL output.

<div class="carousel-item" item-position="1">
    ...
</div>
<div class="carousel-item" item-position="2">
    ...
</div>
<div class="carousel-item" item-position="3">
    ...
</div>

Each div-element represents a Sitecore component at placeholder carousel. Attribute item-position is the current position of the Sitecore component at the placeholder. Keep in mind that Sitecore rules must be executed before a component is visible at the website.

Does anyone know how I could calculate the attribute item-position (1, 2, 3 etc.)?

Thanks a lot.

Jordy

1

There are 1 best solutions below

0
On BEST ANSWER

The easiest way is probably to manually count everytime you execute a component and store the counter value in Sitecore.Context.Items collection, which is shared for one single request. Something like this:

public static int GetComponentCount()
{
    var counter = Sitecore.Context.Items["myCounter"] as int?;
    var count = counter.HasValue ? (int)counter : 0;
    count = count + 1;
    Sitecore.Context.Items["myCounter"] = count;

    return count;
}

Then get the current component count in your components with this method.