Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/docwhat/temple

Sick of sed? Prosaic about perl? Use temple to substitute your variables!
https://github.com/docwhat/temple

go golang substitute template

Last synced: 2 months ago
JSON representation

Sick of sed? Prosaic about perl? Use temple to substitute your variables!

Awesome Lists containing this project

README

        

# Temple

> Sick of `sed`? Peaked about `perl`? Use `temple` to substitute your variables!


GitHub release
GitHub license
GoDoc reference example
Go version

release status
main branch status
Go Report Card

PRs Welcome
GitHub contributors
GitHub issues

## Installation

### Binaries

I have pre-built binaries for several platform already. They are available on the [releases page](https://github.com/docwhat/temple/releases).

### Source

If you have go v1.6 installed, then you can build the binary with the following command:

```bash
go install docwhat.org/temple
```

## Usage

Usage: temple

A simple templating engine

Arguments:
The template file to use ($TEMPLE_TEMPLATE_FILE)

Flags:
-h, --help Show context-sensitive help.
--version Show version information
-d, --data=DATA-FILE A YAML or JSON file to use via the {{data.}} interface ($TEMPLE_DATA_FILE)
-H, --html Use HTML templating instead of text templating ($TEMPLE_USE_HTML)

Note that the `DATA` file must have an object at the top level. You cannot use a bare string or an array.

JSON Example:

```json
{
"key": "value",
"key2": 2
}
```

YAML Example:

```yaml
key: "value"
key2: 2
```

## Template Syntax

For complete documentation, read go's [text/template](https://golang.org/pkg/text/template/) and [html/template](https://golang.org/pkg/html/template/).

### Sprig Functions

Temple supports the complete list of [Sprig functions](http://masterminds.github.io/sprig/).

### Data Sources

* `{{hostname}}` -- The systems fully qualified domain name.
* `{{uid}}` -- `UID` of the user running `temple`.
* `{{gid}}` -- `GID` of the user running `temple`.
* `{{euid}}` -- Effective `UID` of the user running `temple`.
* `{{egid}}` -- Effective `GID` of the user running `temple`.
* `{{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}}`

### Functions

* `{{index 99}}` -- The 99th item of the array ``.
* `{{ | js}}` -- `` escaped/quoted for JavaScript & JSON.
* `{{ | html}}` -- `` escaped/quoted for HTML.
* `{{ | urlquery}}` -- `` escaped/quoted for a URL quoting. i.e. replacing spaces with `+` and using `%NN` syntax.
* `{{ | shellquote}}` -- `` escaped/quoted for POSIX shells.
* `{{ | len}}` -- The length of the ``.

### Flow Control

* `{{if }}true string{{else}}false string{{end}}` -- If/Else syntax. The `{{else}}` is optional.
* `{{range }} item: {{.}} {{else}} The list is empty {{end}}` -- Iterate over ``. The `{{else}}` is optional.

### Miscellaneous

* `{{ -}}` -- Trim whitespace to the right. e.g. `{{1 -}} .0` becomes `1.0`.
* `{{- }}` -- Trim whitespace to the left.
* `{{- -}}` -- Trim whitespace to the right and left.
* `{{/* comment */}}` -- Comments!

## Related Projects

* [gomplate](https://github.com/hairyhenderson/gomplate)

## Thanks

* [@alecthomas](https://github.com/alecthomas) for [kingpin](https://github.com/alecthomas/kingpin)
* [@kballard](https://github.com/kballard) for [go-shellquote](https://github.com/kballard/go-shellquote)
* [@seh](https://github.com/seh) for Go help