Arithmetic in Go templates
Check the documentation about the built-in functions of text/template
:
len
can produce an integer from a string:{{ len "abc" }}
printf
can produce a string of a given length from an integer:{{ printf "%*s" 3 "" }}
slice
can truncate a string to a given length from an integer:{{ slice "abc" 0 2 }}
slice
can truncate a string by a given length from an integer:{{ slice "abc" 1 }}
You can combine both to increment an integer:
{{ len (printf "a%*s" 3 "") }}
will produce:
4
Or to decrement an integer:
{{ len (slice (printf "%*s" 3 "") 1) }}
shows:
2
You can also define templates to reuse pieces of templates.
So we can define 1 argument functions with templates (see on the Go Playground):
{{ define "inc" }}{{ len (printf "%*s " . "") }}{{ end -}}
{{ define "op" }}{{.}} + 1 = {{ template "inc" . }}{{ end -}}
{{ template "op" 2 }}
{{ template "op" 5 }}
shows:
2 + 1 = 3
5 + 1 = 6
We can go further if the input of the template is an array of integers ([]int{4, 2, 7}
):
{{ define "inc" }}{{ len (printf "%*s " . "") }}{{ end -}}
{{ define "op" }}{{.}} + 1 = {{ template "inc" . }}{{ end -}}
{{- range . -}}
{{ template "op" . }}
{{ end -}}
See full example on the Go Playground.
You have to write a custom function to do this.
http://play.golang.org/p/WsSakENaC3
package main
import (
"os"
"text/template"
)
func main() {
funcMap := template.FuncMap{
// The name "inc" is what the function will be called in the template text.
"inc": func(i int) int {
return i + 1
},
}
var strs []string
strs = append(strs, "test1")
strs = append(strs, "test2")
tmpl, err := template.New("test").Funcs(funcMap).Parse(`{{range $index, $element := .}}
Number: {{inc $index}}, Text:{{$element}}
{{end}}`)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, strs)
if err != nil {
panic(err)
}
}
If you happen to be writing a Go template for use in consul-template
, you may find their exposed arithmetic functions useful:
Number: {{add $index 1}}
There's also way to just make a HTML list, however that won't fit for all cases.
<ol>
{{range $index, $element := .Pages}}
<li>Text: {{$element}}</li>
{{end}}
</ol>
And it can produce something like that
- Text: Some page
- Text: Some different page