Skip to content

Generate file based on a json data file and a template file

Notifications You must be signed in to change notification settings

aburdulescu/tpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tpl

Generate file based on a json data file and a template file

Install:

  • install golang
  • then run
go install bandr.me/p/tpl@latest

Usage

Run the tool with -h to see the available options.

Examples

You can find the files used here in the example directory.

The syntax of the template file can be found here.

Data file provided as argument

  • 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.

Data file from stdin

  • 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