2

Is there a way to read the search results count in a Scriban template in a rendering variant of SearchResult in SXA?

I have a requirement that if the search results count is less than 6 the result should be rendered on the page - otherwise we should be displaying a link so that the user can navigate to a page where we will display all the results.

I decided to achieve it thru Scriban by determining the result count and decide whether to render the search result or link.

3
  • The SearchResult scriban is the repeating element in the search results, its not rendering the full result set, so I would be surprised if it passed in the total count of the search results to the rendering.
    – Richard Seal
    Dec 14 '20 at 14:37
  • What are you trying to achieve? Could you explain exactly what you want to display where?
    – Gatogordo
    Dec 16 '20 at 9:17
  • @Gatogordo There is a requirement that if the search results count is lesser than 6 then the result should be rendered there itself or we should be displaying a link so that user can navigate to a page where we will display all the results. I decided to achieve it thru scriban by determining the result count and decide whether to render the search result or link. Dec 16 '20 at 10:42
1

This is not something you can do ootb with the SearchResults component. I think you have a few options to achieve the functionality:

  1. Use the SearchResults component and add some custom JavaScript to your page that counts the results and hides the results and displays a text if needed - this places your logic in the browser and I'm not sure if it's that easy to get a good user experience this way (but I'm no js expert)
  2. Write a custom rule that checks the count and use that with personalization to show the desired component - this will work fine but in case of few results you will have done the query twice
  3. Write a custom Scriban extension to do your query and check the count (https://ggullentops.blogspot.com/2019/11/custom-sxa-scriban-extensions.html) Use that output to show a different component from within your Scriban template.
  4. Go fully custom.. write the query, check the output and display either a text or the results - you can use variants with your own components (https://ggullentops.blogspot.com/2017/03/variants-sxa-sitecore-rendering.html) and also make the component compatible with pagination (https://ggullentops.blogspot.com/2018/12/sxa-pagination-on-custom-component.html)

I think I would go for option 3 or 4...

0

As Richard said it will not be possible for use the count but i would like to know your business logic scenario and see if we can achieve this using some other ways

I had scenarios where i had to show count of jobs for specific location, where because i could not use the SearchResult, i wrote a custom component which eventually called my SXA controller and it called my repository and from there i read the count

And in SXA variant you can call your custom components too so just like you add field and other fields, you can call your custom component and that markup will be shown when your full variant is rendered

enter image description here

1
  • Thank you Daivanga I will try this and let you know. Dec 16 '20 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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