0

I have this list of an array and want to print something if index = 0, but print something else after that. I've looked at this docs but seems not successful... all of them is printing something else instead.

  {{ range $i, $v := .Lists }}
    {{ if $i = 0 -}}
      do something
    {{- else -}}
      do something else
    {{- end -}}
  {{- end }}
rulisastra
  • 311
  • 1
  • 2
  • 14
  • 2
    It's `{{ if eq $i 0 }}`. Go templates do not have support for standard binary comparison operators, you need to use functions, `eq` is a builtin function that does equality comparison. – mkopriva Dec 30 '21 at 10:46
  • At the top of the linked gotmpl documentation it says: *"Gomplate uses the syntax understood by the Go language’s text/template package."* The documentation of which is [here](https://pkg.go.dev/text/template) and for the section on functions, scroll [here](https://pkg.go.dev/text/template#hdr-Functions). – mkopriva Dec 30 '21 at 10:49
  • 1
    wow, it works with function `eq`. @mkopriva thank you so much! – rulisastra Dec 31 '21 at 01:20

1 Answers1

0

try == not =. maybe like this

 {{ if $i == 0 -}}

in documment:

And the following comparison operators are also supported: eq: Equal (==)...

https://docs.gomplate.ca/syntax/#functions

Ahmed
  • 367
  • 2
  • 12
  • no, it can't. it cant be "0" also. it was printing 0 though if I print the `$i` – rulisastra Dec 30 '21 at 12:34
  • in doc ```And the following comparison operators are also supported: eq: Equal (==)... ``` https://docs.gomplate.ca/syntax/#functions – Ahmed Dec 30 '21 at 12:53
  • 1
    yeah, that's why it was wierd. the error was `could not add template: failed to load template clientClient: template: clientClient:127: unexpected "=" in if` – rulisastra Dec 31 '21 at 01:12