Sick of sed
? Peaked about perl
? Use temple
to substitute your variables!
I have pre-built binaries for several platform already. They are available on the releases page.
If you have go v1.6 installed, then you can build the binary with the following command:
go get -u -v docwhat.org/temple
usage: temple [<flags>] <template>
Fast and simple templating engine
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--version Show application version.
-j, --json-data=JSON-DATA A JSON file to use via the {{json.<foo>}} interface (Env: TEMPLE_JSON_DATA_FILE)
-H, --html Use HTML templating instead of text templating (Env: TEMPLE_HTML)
Args:
<template> A Go Template file.
Note that the JSON file must be an object at the top level. Example:
{
"key": "value",
"key2": 2
}
For complete documentation, read go's text/template and html/template.
Temple supports the complete list of Sprig functions.
{{hostname}}
-- The systems fully qualified domain name.{{uid}}
--UID
of the user runningtemple
.{{gid}}
--GID
of the user runningtemple
.{{euid}}
-- EffectiveUID
of the user runningtemple
.{{egid}}
-- EffectiveGID
of the user runningtemple
.{{pwd}}
-- The current working directory.{{json}}
-- Access to your JSON data. Use dot notation to get access to items. e.g.{{json.authors.greenwood.first_name}}
{{index <expr> 99}}
-- The 99th item of the array<expr>
.{{<expr> | js}}
--<expr>
escaped/quoted for JavaScript & JSON.{{<expr> | html}}
--<expr>
escaped/quoted for HTML.{{<expr> | urlquery}}
--<expr>
escaped/quoted for a URL quoting. i.e. replacing spaces with+
and using%NN
syntax.{{<expr> | shellquote}}
--<expr>
escaped/quoted for POSIX shells.{{<expr> | len}}
-- The length of the<expr>
.
{{if <expr>}}true string{{else}}false string{{end}}
-- If/Else syntax. The{{else}}
is optional.{{range <array>}} item: {{.}} {{else}} The list is empty {{end}}
-- Iterate over<array>
. The{{else}}
is optional.
{{<expr> -}}
-- Trim whitespace to the right. e.g.{{1 -}} .0
becomes1.0
.{{- <expr>}}
-- Trim whitespace to the left.{{- <expr> -}}
-- Trim whitespace to the right and left.{{/* comment */}}
-- Comments!
- @alecthomas for kingpin
- @kballard for go-shellquote
- @seh for Go help