gt (JSON go-templating renderer)

Description
A command-line tool to render Go templates from JSON input data.
The tool is invoked with the path to the template file. If a second argument is provided, it is treated as the path to a JSON file to use as input for the template. If no second argument is provided, the tool reads JSON input from stdin.
Features
Builtin functions
env
The env function returns the value of the specified environment variable.
{{ env "ENV_VAR_NAME" }}
Expected output:
value
now
The now function returns the current date and time.
{{ now }}
Expected output:
2021-09-01 12:00:00
date
The date function formats a date and time value using a specified layout.
{{ now | date "2006-01-02"}}
Expected output:
2021-09-01
add
The add function adds two numbers.
{{ 1 | add 2 }}
Expected output:
3
sub
The sub function subtracts two numbers.
{{ 2 | sub 1 }}
Expected output:
1
mul
The mul function multiplies two numbers.
{{ 1 | mul 2 }}
Expected output:
2
div
The div function divides two numbers.
{{ 1 | div 2 }}
Expected output:
0.5
upper
The upper function converts a string to uppercase.
{{ "hello" | upper }}
Expected output:
HELLO
lower
The lower function converts a string to lowercase.
{{ "HELLO" | lower }}
Expected output:
hello
trim
The trim function removes leading and trailing whitespace from a string.
{{ " hello " | trim }}
Expected output:
hello
trimleft
The trimleft function removes leading whitespace from a string.
{{ " hello " | trimleft }}
Expected output:
hello
trimright
The trimright function removes trailing whitespace from a string.
{{ " hello " | trimright }}
replace
The replace function replaces all occurrences of a substring in a string with another substring.
{{ "hello world" | replace "world" "go" }}
Expected output:
hello go
contains
The contains function returns true if a string contains a specified substring.
{{ "hello world" | contains "world" }}
Expected output:
true
hasprefix
The hasprefix function returns true if a string has a specified prefix.
{{ "hello world" | hasprefix "hello" }}
Expected output:
true
hassuffix
The hassuffix function returns true if a string has a specified suffix.
{{ "hello world" | hassuffix "world" }}
Expected output:
true
indexof
The indexof function returns the index of the first occurrence of a substring in a string.
{{ "hello world" | indexof "world" }}
Expected output:
6
lastindexof
The lastindexof function returns the index of the last occurrence of a substring in a string.
{{ "hello world" | lastindexof "o" }}
Expected output:
7
reverse
The reverse function reverses a string.
{{ "hello" | reverse }}
Expected output:
olleh
substr
The substr function returns a substring of a string.
{{ "hello world" | substr 6 }}
Expected output:
world
escapeString
The escapeString function escapes special characters in a string.
{{ "hello <world>" | escapeString }}
Expected output:
hello <world>
len
The len function returns the length of a string or array.
{{ "hello" | len }}
Expected output:
5
regexFind
The regexFind function returns the first match of a regular expression in a string.
{{ "hello world" | regexFind ".{4}$" }}
Expected output:
world
empty
The empty function returns true if a string or array is empty.
{{ "" | empty }}
Expected output:
true
Installation
Download from release page.
Usage
mytemplate.tpl:
{{ range . }}
{{ .name }} is {{ .age }} years old
{{ end }}
mydata.json:
[
{
"name": "Alice",
"age": 30
},
{
"name": "Bob",
"age": 25
}
]
Render from file
gt mytemplate.tpl mydata.json
Render from stdin
cat mydata.json | gt mytemplate.tpl
Expected output:
Alice is 30 years old
Bob is 25 years old
Contributing
Contributions are welcome! Please follow the guidelines in CONTRIBUTING.md.
License
This project is licensed under the MIT License.