Skip to content
Apply go template over files ending with .template in the current directory
Go HTML Other
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
.vscode
collections
docs
docs_tests
hcl
json
template
utils
xml
yaml
.gitignore
.gitmodules
.goreleaser.yml
CODE_OF_CONDUCT.md
LICENSE
Makefile
README.md
context.go
go.mod
go.sum
main.go
main_test.go
render-doc
test.sh
utilities.go
utilities_test.go

README.md

gotemplate

Build Status codecov 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

More examples and statement in the documentation

You can’t perform that action at this time.