0

I have a Search Result Listing Variant created using Scriban. This will render a card from each of the items got from the scope query. However, I want to get the index of items at the scriban template.

Here is my current Search Result Listing

<div class="search-result-card" data-item-index="{{i_item.index}}">
  ...

</div>

Lets say my Scope is returning 5 items, Basically I want to get the index of the current item and put it on a data-attribute. But i_item context that is exposed at scriban is pointing to current item.

Is there no way to get that index?

Sorry if my explanation is a it confusing.

Thanks all

1 Answer 1

2

I am afraid there is no way to do it because variant is used to generate each result separately not the whole list.

This is a place where items from index are prepared

Sitecore.XA.Feature.Search.Controllers.SearchController

list = (IEnumerable<Result>) source.Select<SearchItem, Result>((Func<SearchItem, Result>) (i => new Result(i, variant))).ToList<Result>();

Sitecore.XA.Feature.Search.Models.Result

Inside Result model output HTML is created this way.

this.Html = service.RenderVariant(variant, searchItem.Item);

As you can see only variant and single item are passed to service thus there is no way to tell the index.

Solution - workaround

Not ideal because you cannot do it via variant but if you really need it I would suggest doing it in JS.

Not sure which one will work better for you:

  • try to attach to event responsible for binding search results (XA.component.search.vent.on("results-loaded", this.resultsLoaded.bind(this)); in component-search-results.js
  • override render function in component-search-results.js
1
  • Still a Huge Thanks for this! At the very least this helped me to get on the right track! Thanks mate!
    – keysl
    Commented Dec 3, 2021 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.