2

I tried to do this test to figure out how to make conditions like this:

<h1>Country Index</h1> <style> .odd{ background: orange; color: black; font-size: 1.5em; } .even{ background: rgb(0, 121, 235); color: white; font-size: 1.5em; } </style> <ul> {{ range $index, $item := .Tee }} {{ if $index % 2 == 0 }} <li class="even">{{ $index }} - {{ $item }}</li> {{ else }} <li class="odd">{{ $index }} - {{ $item }}</li> {{ end }} {{ end }} </ul> 

I got this error "unexpected "%" in operand".

Any suggestions to solve this?

10

2 Answers 2

3

You can't use operators like +, -, *, /, or % in templates, unfortunately. Instead you have to write custom functions and use a funcMap to bring them into your template.

Here's an example on the Go Playground that detects even integers applied to a slightly modified version of your template text.

https://play.golang.org/p/LWEhE_TI31o

Sign up to request clarification or add additional context in comments.

Comments

2

You can create bool helper variable and change its state every iteration.

{{$odd := true}} {{range $data := .Tee}} {{if $odd}} <p>action for odd.</p> {{else}} <p>action for not odd.</p> {{end}} {{$odd = not $odd}} {{end}} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.