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

Note: See also conv.ToBool for a more flexible variant.

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.Default

Alias: default

Provides a default value given an empty input. Empty inputs are 0 for numeric types, "" for strings, false for booleans, empty arrays/maps, and nil.

Note that this will not provide a default for the case where the input is undefined (i.e. referencing things like .foo where there is no foo field of .), but conv.Has can be used for that.

Example

$ gomplate -i '{{ "" | default "foo" }} {{ "bar" | default "baz" }}'
foo bar

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

Reports whether a given object has a property with the given key, or whether a given array/slice contains the given value. Can be used with if to prevent the template from trying to access a non-existent property in an object.

Usage

conv.Has in item

Arguments

name description
in (required) The object or list to search
item (required) The item to search for

Examples

$ gomplate -i '{{ $l := slice "foo" "bar" "baz" -}}
there is {{ if has $l "bar" }}a{{else}}no{{end}} bar'
there is a bar
$ export DATA='{"foo": "bar"}'
$ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}}
{{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}'
bar
$ export DATA='{"baz": "qux"}'
$ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}}
{{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}'
THERE IS NO FOO

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.ToBool

Converts the input to a boolean value. Possible true values are: 1 or the strings "t", "true", or "yes" (any capitalizations). All other values are considered false.

Usage

conv.ToBool input 
input | conv.ToBool  

Arguments

name description
input (required) The input to convert

Examples

$ gomplate -i '{{ conv.ToBool "yes" }} {{ conv.ToBool true }} {{ conv.ToBool "0x01" }}'
true true true
$ gomplate -i '{{ conv.ToBool false }} {{ conv.ToBool "blah" }} {{ conv.ToBool 0 }}'
false false false

conv.ToBools

Converts a list of inputs to an array of boolean values. Possible true values are: 1 or the strings "t", "true", or "yes" (any capitalizations). All other values are considered false.

Usage

conv.ToBools input 
input | conv.ToBools  

Arguments

name description
input (required) The input array to convert

Examples

$ gomplate -i '{{ conv.ToBools "yes" true "0x01" }}'
[true true true]
$ gomplate -i '{{ conv.ToBools false "blah" 0 }}'
[false false false]

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

conv.ToString

Converts the input (of any type) to a string

conv.ToStrings

Converts the inputs (of any type) to an array of strings