Generate file based on a json data file and a template file
- install golang
- then run
go install bandr.me/p/tpl@latest
Run the tool with -h
to see the available options.
You can find the files used here in the example
directory.
The syntax of the template file can be found here.
- template file(
example.tpl
)
My name is {{.name}}.
My friends are:
{{range .friends}}
- {{.}}
{{end}}
The answer to the "Ultimate Question to Life, the Universe, and Everything" is {{.answer}}.
- data file(
example.json
)
{
"name": "Arthur Dent",
"friends": [
"Ford Prefect",
"Zaphod Beeblebrox",
"Marvin",
"Trillian"
],
"answer": 42
}
- run the tool using the files above
tpl -t example.tpl example.json
My name is Arthur Dent.
My friends are:
- Ford Prefect
- Zaphod Beeblebrox
- Marvin
- Trillian
The answer to the "Ultimate Question to Life, the Universe, and Everything" is 42.
- template file(
example2.tpl
)
Name: {{.name}}
Race: {{.race}}
- data file(
example2.json
)
{
"persons": [
{
"name": "Arthur Dent",
"race": "human"
},
{
"name": "Ford Prefect",
"race": "alien"
}
]
}
- run the tool and pass the data using jq
jq ".persons[0]" | tpl -t example.tpl
Name: Arthur Dent
Race: human
jq ".persons[1]" | tpl -t example.tpl
Name: Ford Prefect
Race: alien