conversion functions

These are a collection of functions that mostly help converting from one type to another - generally from a string to something else, and vice-versa.

conv.Bool

Alias: bool

Converts a true-ish string to a boolean. Can be used to simplify conditional statements based on environment variables or other text input.

Example

input.tmpl:

{{if bool (getenv "FOO")}}foo{{else}}bar{{end}}
$ gomplate < input.tmpl
bar
$ FOO=true gomplate < input.tmpl
foo

conv.Slice

Alias: slice

Creates a slice. Useful when needing to range over a bunch of variables.

Example

input.tmpl:

{{range slice "Bart" "Lisa" "Maggie"}}
Hello, {{.}}
{{- end}}
$ gomplate < input.tmpl
Hello, Bart
Hello, Lisa
Hello, Maggie

conv.Has

Alias: has

Has reports whether or not a given object has a property with the given key. Can be used with if to prevent the template from trying to access a non-existent property in an object.

Example

Let’s say we’re using a Vault datasource…

input.tmpl:

{{ $secret := datasource "vault" "mysecret" -}}
The secret is '
{{- if (has $secret "value") }}
{{- $secret.value }}
{{- else }}
{{- $secret | toYAML }}
{{- end }}'

If the secret/foo/mysecret secret in Vault has a property named value set to supersecret:

$ gomplate -d vault:///secret/foo < input.tmpl
The secret is 'supersecret'

On the other hand, if there is no value property:

$ gomplate -d vault:///secret/foo < input.tmpl
The secret is 'foo: bar'

conv.Join

Alias: join

Concatenates the elements of an array to create a string. The separator string sep is placed between elements in the resulting string.

Example

input.tmpl

{{ $a := `[1, 2, 3]` | jsonArray }}
{{ join $a "-" }}
$ gomplate -f input.tmpl
1-2-3

conv.URL

Alias: urlParse

Parses a string as a URL for later use. Equivalent to url.Parse

Example

input.tmpl:

{{ $u := conv.URL "https://example.com:443/foo/bar" }}
The scheme is {{ $u.Scheme }}
The host is {{ $u.Host }}
The path is {{ $u.Path }}
$ gomplate < input.tmpl
The scheme is https
The host is example.com:443
The path is /foo/bar

conv.ParseInt

Parses a string as an int64 for later use. Equivalent to strconv.ParseInt

Example

input.tmpl:

{{ $val := conv.ParseInt (getenv "HEXVAL") 16 32 }}
The value in decimal is {{ $val }}
$ HEXVAL=7C0 gomplate < input.tmpl

The value in decimal is 1984

conv.ParseFloat

Parses a string as an float64 for later use. Equivalent to strconv.ParseFloat

Example

input.tmpl:

{{ $pi := conv.ParseFloat (getenv "PI") 64 }}
{{- if (gt $pi 3.0) -}}
pi is greater than 3
{{- end }}
$ PI=3.14159265359 gomplate < input.tmpl
pi is greater than 3

conv.ParseUint

Parses a string as an uint64 for later use. Equivalent to strconv.ParseUint

input.tmpl:

{{ conv.ParseInt (getenv "BIG") 16 64 }} is max int64
{{ conv.ParseUint (getenv "BIG") 16 64 }} is max uint64
$ BIG=FFFFFFFFFFFFFFFF gomplate < input.tmpl
9223372036854775807 is max int64
18446744073709551615 is max uint64

conv.Atoi

Parses a string as an int for later use. Equivalent to strconv.Atoi

input.tmpl:

{{ $number := conv.Atoi (getenv "NUMBER") }}
{{- if (gt $number 5) -}}
The number is greater than 5
{{- else -}}
The number is less than 5
{{- end }}
$ NUMBER=21 gomplate < input.tmpl
The number is greater than 5

conv.ToInt64

Converts the input to an int64.

$ gomplate -i '{{conv.ToInt64 "9223372036854775807"}}'
9223372036854775807

conv.ToInt

Converts the input to an int. This is similar to conv.Atoi, but handles booleans and numbers as well as strings.

$ gomplate -i '{{conv.ToInt (gt 1 2)}}'
0

conv.ToInt64s

Converts the inputs to an array of int64s

conv.ToInts

Converts the inputs to an array of ints

conv.ToFloat64

Converts the input to a float64

conv.ToFloat64s

Converts the inputs to an array of float64s