0
  <tr>
      <td>rank</td>
      <td>abc</td>
  </tr>
  {{ range $index,$abc := .abc }}
     <tr>
      <td>{{$index}}</td> // 0
      <td>{{$abc}}</td>
     </tr>
  {{end}}
  1. how to {{$index}} starts with 1

    {{add $index 1}} - unction "add" not defined

    {{$index + 1}} - illegal number syntax: "+"

Revllllł1
  • 13
  • 2
  • Have you try passing your own `add` function to the template data? – Koala Yeung Oct 16 '19 at 04:34
  • You can assign a variable "add" to your controller's [ViewArgs](https://godoc.org/github.com/revel/revel#Controller.ViewArgs) that returns a +1 value of the input number. – Koala Yeung Oct 16 '19 at 04:39

1 Answers1

0

You can pass a custom function into your controller's ViewArgs as a variable.

controller.ViewArgs["addOne"] = func (i int64) {
    return i+1
}

You can then use $.addOne to access the function in a loop. To use it as a function, you have to add a call keyword before it:

  <tr>
      <td>rank</td>
      <td>abc</td>
  </tr>
  {{ range $index, $abc := .abc }}
     <tr>
      <td>{{call $.addOne $index}}</td> // $index + 1
      <td>{{$abc}}</td>
     </tr>
  {{end}}
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50