Show string if array index is even

For a new theme I need to insert a CSS class with each even index of an array. The array holds the events of a timeline that flips the side with following event.

So, if the current array index modulo 2 equals 0, insert the CSS class. The difficulty is to get this working with the template functions. It should look similar to this (where currentIndex is a placeholder):

{{ if mod currentIndex 2 eq 0 }}class="timeline-inverted"{{ end }}


Now I found a way to get the current index but Hugo still threws an error because mod takes two arguments but gets four (currendIndex, eq and 0).

{{ range $i, $e := }}
    <li {{ if mod $i 2 eq 0 }}class="timeline-inverted"{{ end }}>
1 Like

Your syntax is wrong. This should be correct:

{{ if eq (mod $i 2) 0 }}

But I suspect this is better:

{{ if modBool $i 2 }}
1 Like

Thanks for your answer @bep.