Gravatar for

Question by DEEPTHI KATTA, Nov 1, 2016 8:11 PM

Access Index or seach result number in underscore template

Okay, we have a weird design requirement.

On each search result item based on number(index) of search result item, we need to display different fields related to the search result item.

Like say:

  • Index 0
    • Image Field - "My First Image Field"
    • Title - "My First Title"
    • Description - "My First Description"
  • Index % 6 = 0
    • Image Field - "My Second Image Field"
    • Title - "My Second Title"
    • Description - "My Second Description"

Is this possible?

I am unsure how to grab current index from raw, is this available?

1 Reply
Gravatar for

Answer by François Lachance-Guillemette, Nov 2, 2016 7:53 AM

Hi @Dipsindol :)

Yes, the index is accessible in the underscore template using {{= index}}

You can then apply any regular underscore template logic to that index, like your modulo operator.

For instance, the following code would only render on the first result:

{{ if(index == 0) { }}
    <div>I am the first result</div>
{{ } }}

Hope this helps! :)


Gravatar for

Comment by DEEPTHI KATTA, Nov 3, 2016 3:07 PM

This is cool. We will try and keep you posted.

Ask a question