gotmplx implements the go Template Engine and makes it available as a standalone application. This application extends the engine with various functions and supports injection of environment variables, csv data and flag values.
Since this repository provides versioned versions with binaries, you can decide whether to download such a binary or install the application using the go install command.
Example of using the go install command:
go install github.com/programmfabrik/gotmplx
Short | Flag | Type | Description |
---|---|---|---|
--csv |
[]string | Parse and use csv file rows in template (--csv key=file) | |
--yml |
[]string | Parse and use yml file in template (--yml key=file) | |
--json |
[]string | Parse and use json file in template (--json key=file) | |
--dump |
bool | Dump parsed data which is passed to template. | |
-o |
--output |
string | Send output to file, use "-" for stdout (default) |
--html |
bool | Render template as HTML, defaults to text rendering. | |
-e |
--eval |
string | Parse this text instead of file argument (--eval "{{ .Var.myvar }}" |
-h |
--help |
Help for gotmplx | |
--var |
[]string | Parse and use variable in template (--var myvar=value) | |
-l |
-template-delim-left |
string | Use this string as go template left delimiter |
-r |
-template-delim-right |
string | Use this string as go template right delimiter |
./gotmplx --var "key=value" --eval "My key value: {{.var.key}}"
Result:
My key value: value
Create the csv file:
cat <<EOF > test.csv
name,id
henk,10
EOF
./gotmplx --csv "mycsvfile=test.csv" --eval "My csv value: {{.csv.mycsvfile}}"
Result:
My csv value: [map[id:10 name:henk]]
Since gotmplx injects the environment into the .Env
key, you can access any environment variable from a template.
./gotmplx --eval "My shell: {{.env.SHELL}}"
Result:
My shell: /bin/bash
Create the template file:
cat <<EOF > my.tmpl.txt
Name: {{.Var.Name}}
ID: {{.Var.ID}}
Shell: {{.Env.SHELL}}
EOF
Execute gotmplx:
./gotmplx --var "Name=dummy" --var "ID=10" my.tmpl.txt
Result:
Name: dummy
ID: 10
Shell: /bin/bash
This application supports custom template functions using the sprig-github template function library. Visit the Godoc for sprig-godoc to see all the available functions that are available.