https://github.com/coveooss/gotemplate
Apply go template over files ending with .template in the current directory
https://github.com/coveooss/gotemplate
Last synced: about 2 months ago
JSON representation
Apply go template over files ending with .template in the current directory
- Host: GitHub
- URL: https://github.com/coveooss/gotemplate
- Owner: coveooss
- License: mit
- Created: 2017-05-15T16:48:26.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2025-03-13T02:14:38.000Z (3 months ago)
- Last Synced: 2025-03-29T16:05:36.116Z (2 months ago)
- Language: Go
- Size: 2.32 MB
- Stars: 72
- Watchers: 11
- Forks: 12
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# gotemplate
[](https://github.com/coveooss/gotemplate/actions)
[](https://goreportcard.com/report/github.com/coveooss/gotemplate)
[](https://coveooss.github.io/gotemplate/)## Description
Apply template over files ending with `.template` in the current directory. Every matching `*.ext.template` file will render a file named `*.generated.ext`. It is also possible to overwrite the original files.
### Functions
Supports over a hundred functions:
- [Go template](https://golang.org/pkg/text/template)
- [Sprig](https://github.com/Masterminds/sprig)
- Advanced serialization and deserialization in JSON, YAML, XML and HCL
- Looping and flow control functions of all kinds
- Plus a whole bunch implemented in this repository### Syntax
Supports two distinct syntaxes (usable at the same time or individually)
Here are the statements to generate the following output:
```text
Hello
World
```Note: The `-` character trims whitespace. Otherwise, all lines are printed out as blank lines
#### Regular gotemplate
```go
{{- $test := list "Hello" "World" }}
{{- range $word := $test }}
{{ $word }}
{{- end }}
```#### Razor
```go
@{test} := list("Hello", "World")
@-foreach($word := $test)
@{word}
@-end foreach
```### Using variables
Variables can be imported from various formats (YAML, JSON and HCL) and set as CLI arguments and then used in templates. Here's an example:
`vars.json`
```json
{
"my_var": "value"
}
```Script:
```bash
gotemplate --var my_var2=value2 --import vars.json '{{ .my_var }} {{ .my_var2 }}'
>>> value value2
```More examples and statement in the [documentation](https://coveooss.github.io/gotemplate/)
Library usage [documentation](https://pkg.go.dev/github.com/coveooss/gotemplate/v3/template)