https://github.com/euank/gotmpl
The simplest templating possible, in library and cli form
https://github.com/euank/gotmpl
Last synced: about 1 year ago
JSON representation
The simplest templating possible, in library and cli form
- Host: GitHub
- URL: https://github.com/euank/gotmpl
- Owner: euank
- License: apache-2.0
- Created: 2016-02-11T03:11:14.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-01-28T22:09:22.000Z (over 7 years ago)
- Last Synced: 2025-03-24T20:51:12.794Z (about 1 year ago)
- Language: Go
- Size: 13.7 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gotmpl
A stupid-simple template substitution tool.
## Template language
This code supports the following three features:
1. `${var}` — a template variable which will be replaced with the value of `var`
1. `\$` — an escape, which will be replaced with a `$`
1. `\\` — an escape, which will be replaced with a `\`
No other special syntax is supported.
## Usage
### As a CLI tool
By default, the `gotmpl` cli tool will resolve template variables from the current environment. As an argument, it takes a file to template and prints the result to stdout.
For example:
```sh
$ cat input_file
hello ${VAR}
$ export VAR=world
$ gotmpl input_file
hello world
$ gotmpl input_file > output_file
$ cat output_file
hello world
```
### As a library
A template may be evaluated by providing anything which satisfies the `Lookup` interface. The most trivial thing to use is a go map via the `MapLookup`:
```go
result, err := gotmpl.TemplateString("foo is ${foo}", gotmpl.MapLookup(map[string]string{"foo": "value"}))
fmt.Println(result)
// Prints: foo is value
```
# See Also
* [envsubst](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html)
* [Apache Commons StrSubstitutor](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/StrSubstitutor.html)
* sh