A tool to create files rendered from go templates and json
Usage: tplr [-f] [-o <output file>] [-d <data file>] [-t <template file>] [inline template]
Usage: tplr [-h|-v]
Where:
-o <output file> is a file to write to (default: stdout)
-d <data file> is a json file containing the templated variables (default: stdin)
-t <template file> is a file using the go templating notation.
If this is not specified, the template is taken from the remaining program args
Options:
-f If the destination file already exits, overwrite it. (default is to do nothing)
Information:
-h Prints this messge
-v Prints the program version number and exits
The templating markup language used is defined by the go text/template package
Using go
go install github.com/mantidtech/tplr/cmd/tplr@latest
echo '{"to":"World"}' | tplr 'Hello {{.to}}!'
# displays:
Hello World!
cat << EOF > sample.tpl
<html>
<head>
<title>{{ .title }}</title>
<head>
<body>
<ul>
{{- range .items -}}
{{- include "listItem" . | indent 3 -}}
{{- end }}
</ul>
</body>
</html>
{{- define "listItem" }}
<li>
{{.}}
</li>
{{- end -}}
EOF
cat << EOF > sample.json
{
"title": "My List Of Doom",
"items": [
"Acquire lair",
"Acquire henchmen",
"Acquire doomsday weapon"
]
}
EOF
tplr -d sample.json -t sample.tpl -o sample.html
cat sample.html
# displays:
<html>
<head>
<title>My List Of Doom</title>
<head>
<body>
<ul>
<li>
Acquire lair
</li>
<li>
Acquire henchmen
</li>
<li>
Acquire doomsday weapon
</li>
</ul>
</body>
</html>
NOTE: tplr
uses the text/template and not the html/template package.
This means there's no special translation of html elements, and therefore output isn't protected against code injection
-- so trust your data source if you intend to use tplr
in any sort of public facing html generation process.
In addition to the standard template functions,
tplr
introduces several collections of functions
strings
- Operations on strings (and pipelines convertable to strings)list
- Operations to operate on lists (arrays) of valueslogic
- Logical operationsmath
- mathematical operatorstime
- time and date methodsencoding
anddecoding
- For marshalling and unmarshalling data structurestemplates
- Meta-functions for template processingconsole
- Operations specific to processing templates to a terminal
ToColumns formats the given TEXT
to not take more than NUMBER
characters per line,
splitting on the space before the word that would take the line over
Note: Embedded newlines have no special treatment, so text containing them could look wonky. Either strip them first, or break the input into multiple strings and process individually
eg
{{ toColumns 5 "a b c d e f g" }}
produces
a b c
d e f
g
Surrounds the pipeline with bracket pairs taken from the string BRACKETPAIR
BRACKETPAIR
must be an even-length string. The first half is used as the opening bracket and the second as closing.
eg
{{ bracketWith "<>" "html" }}
produces
<html>
Surrounds the PIPELINE
with (
& )
.
Equivalent to {{ bracketWith "()" PIPELINE }}
.
Prefixes all lines in the string STRING
with COUNT
times the prefix string PREFIX
.
eg:
{{ prefix "> " 2 "Foo" }}
produces:
> > Foo
Appends all lines in the string STRING
with COUNT
times the suffix string SUFFIX
.
Indents all lines in the string STRING
with COUNT
spaces.
Equivalent to {{ prefix " " COUNT STRING }}
.
eg
{{- indent 2 "Hello\nWorld" -}}
produces
Hello
World
Indents all lines in the string STRING
with COUNT
tabs.
Equivalent to {{ prefix "\t" COUNT STRING }}
.
eg
{{- indent 2 "Hello\nWorld" -}}
produces
Hello
World
Splits CONTENT
into a list of strings on occurrences of the string GLUE
.
Returns PIPELINE
with a width of at least COUNT
characters, adding spaces at the front to pad out the string.
Returns PIPELINE
with a width of at least COUNT
characters, adding spaces at the end to pad out the string.
Returns the arg with the first character capitalised. There is no effect to other characters.
Returns STRING
with the all characters converted to lowercase.
Returns STRING
with the all characters converted to uppercase.
Returns the ARG
printed COUNT
number of times.
Returns a space character.
If COUNT
is supplied, returns the given number of spaces.
Equivalent to {{ rep COUNT " " }}
.
Returns a tab character.
If COUNT
is supplied, return the given number of tabs.
Equivalent to {{ rep COUNT "\t" }}
.
Returns a newline character.
If COUNT
is supplied, return the given number of newlines.
Equivalent to {{ rep COUNT "\n" }}
.
Returns the name of the Go type for the underlying variable of the argument PIPELINE
.
Convert the given STRING
to camelCase.
eg:
{{- camelCase "foo bar" -}}
produces:
fooBar
Convert the given STRING
to dotCase.
eg:
{{- dotCase "foo bar" -}}
produces:
foo.bar
Convert the given STRING
to kebabCase.
eg:
{{- kebabCase "foo bar" -}}
produces:
foo-bar
Convert the given STRING
to pascalCase.
eg:
{{- pascalCase "foo bar" -}}
produces:
FooBar
Convert the given STRING
to screamingSnakeCase.
eg:
{{- screamingSnakeCase "foo bar" -}}
produces:
FOO_BAR
Convert the given STRING
to snakeCase.
eg:
{{- snakeCase "foo bar" -}}
produces:
foo_bar
Convert the given STRING
to titleCase.
eg:
{{- titleCase "foo html" -}}
produces:
Foo Html
Convert the given STRING
to titleCase, additionally using the set of words in ABBREV
to convert words in STRING
to fully uppercase.
eg:
{{- titleCase (list "html") "foo html" -}}
produces:
Foo HTML
Convert the given STRING
to toWords.
eg:
{{- toWords "foo_bar_baz" -}}
produces:
foo bar baz
Quotes the given STRING
with single quotes.
eg:
{{- q "foo" -}}
produces:
'foo'
Quotes the given STRING
with double quotes.
eg:
{{- qq "foo" -}}
produces:
"foo"
Quotes the given STRING
with back-quotes.
eg:
{{- qb "foo" -}}
produces:
`foo`
Trims whitespace from the start and end of STRING
.
eg:
X{{- trim " foo " -}}X
produces:
XfooX
Returns the 'kind' of the PIPELINE
.
eg:
{{ typeKind 6 }}
{{ typeKind "foo" }}
{{ list "A" "B" "C" | typeKind }}
produces:
int
string
slice
Removes up to COUNT
spaces from the start of every line in STRING
.
Returns the current time as a string in the given FORMAT
or time.RFC3339
if none is specified.
Time formats are specified using standard go formatting as defined at https://pkg.go.dev/time#pkg-constants
eg:
{{ now }}
{{ now "Mon Jan _2" }}
{{ now "15:04:05 MST" }}
produces:
2021-09-16T21:41:58Z10:00
Thu Sep 16
21:41:58 AEST
These methods work when the value of the pipeline is an array or slice (and return an error otherwise)
Creates a new list with the given set of items.
Returns the first item in the list.
Returns the last item of a list.
Returns a list of everything except for the first item.
Adds an item to the end of a list, returning the new list.
Adds an item to the start of a list, returning the list.
Returns true
if the item are present in the list.
Returns a list with all instances of all item removed from it.
Concatenates the args into string joined by the string GLUE
.
Concatenates the args into a single string.
Equivalent to {{ joinWith "" ARG }}
.
Returns true
if the pipeline is empty (ie the zero
value of its type) OR
if it's a pointer and the dereferenced value is zero, OR
if the type of the pipeline has a length
(e.g. array, slice, map, string), and the length is zero
Returns ARG_n
if all the arguments evaluate to non-zero or ""
otherwise.
Returns the first argument that evaluates to non-zero (from left to right) or ""
if none do.
Returns VALUE
if COND
is a zero value, otherwise it returns COND
.
eg:
{{ "thing" | whenEmpty "something else" }}
{{ "" | whenEmpty "something else" }}
Produces:
thing
something else
Returns VALUE
if COND
is not zero, otherwise it returns an empty string.
eg:
X{{ "thing" | when "something else" }}X
X{{ "" | when "something else" }}X
Produces:
Xsomething elseX
XX
Converts the given arg to base64 encoding
Decode the given base64 ARG
Converts the given arg to a JSON string
Pretty-prints the pipeline. Returns an error if the arg isn't valid JSON.
Each element in the JSON object or array begins on a new line,
indented by one or more copies of INDENT
according to the nesting depth of the object.
Converts the given PIPELINE
to a YAML string
Works like template
, but allows usage within pipelines.
eg:
{{- include "foo" . | indent 6 -}}
Returns the width of the terminal if available or zero otherwise.
You can also import the template functions to use with your own templating code.
go get github.com/mantidtech/tplr/functions
and use:
import github.com/mantidtech/tplr/functions
A convenience function, functions.All(t *template.Template)
,
is provided to return all the functions as a template.FuncMap
docs are at https://pkg.go.dev/github.com/mantidtech/tplr/functions
-
Process multiple templates and/or datasets at once
-
Greatly expand functions, including for
- Time
- Numbers
- Anything else people might find useful
-
Less basic error messages/presentation
(c) 2020 - Julian Peterson <code@mantid.org>
- MIT Licensed