gotemplate #

Build Status Go Report Card Documentation

Description #

Apply template over files ending with .template in the current directory. Every matching *.ext.template file will render a file named *.generated.ext. It is also possible to overwrite the original files.

Functions #

Supports over a hundred functions:

  • Go template
  • Sprig
  • Advanced serialization and deserialization in JSON, YAML, XML and HCL
  • Looping and flow control functions of all kinds
  • Plus a whole bunch implemented in this repository

Syntax #

Supports two distinct syntaxes (usable at the same time or individually)

Here are the statements to generate the following output:

Hello
World

Note: The - character trims whitespace. Otherwise, all lines are printed out as blank lines

Regular gotemplate #

{{- $test := list "Hello" "World" }}
{{- range $word := $test }}
{{ $word }}
{{- end }}

Razor #

@{test} := list("Hello", "World")
@-foreach($word := $test)
@{word}
@-end foreach

Using variables #

Variables can be imported from various formats (YAML, JSON and HCL) and set as CLI arguments and then used in templates. Here’s an example:

vars.json

{
  "my_var": "value"
}

Script:

gotemplate --var my_var2=value2 --import vars.json '{{ .my_var }} {{ .my_var2 }}'
  >>> value value2

Library usage documentation