Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nicolasparada/go-tmpl-renderer
An opinionated HTML template renderer for Golang
https://github.com/nicolasparada/go-tmpl-renderer
go golang html template
Last synced: 6 days ago
JSON representation
An opinionated HTML template renderer for Golang
- Host: GitHub
- URL: https://github.com/nicolasparada/go-tmpl-renderer
- Owner: nicolasparada
- License: isc
- Created: 2024-02-18T22:17:48.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-28T03:09:33.000Z (about 2 months ago)
- Last Synced: 2024-10-29T23:49:56.183Z (17 days ago)
- Topics: go, golang, html, template
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go Reference](https://pkg.go.dev/badge/github.com/nicolasparada/go-tmpl-renderer.svg)](https://pkg.go.dev/github.com/nicolasparada/go-tmpl-renderer)
# Golang Template Renderer
An opinionated HTML template renderer for Golang.
## Install
```bash
go get github.com/nicolasparada/go-tmpl-renderer
```## Usage
`templates/includes/layout.tmpl`
```handlebars
Golang Template Renderer Demo{{template "header.tmpl" .}}
{{block "content" .}}{{end}}```
`templates/includes/header.tmpl`
```handlebars
```
`templates/welcome.tmpl`
```handlebars
{{template "layout.tmpl" .}}{{define "content"}}
Welcome
{{end}}```
`main.go`
```go
package mainimport (
// other imports
tmplrenderer "github.com/nicolasparada/go-tmpl-renderer"
)//go:embed templates/includes/*.tmpl templates/*.tmpl
var templatesFS embed.FSfunc main() {
renderer := &tmplrenderer.Renderer{
FS: templatesFS,
BaseDir: "templates",
IncludePatters: []string{"includes/*.tmpl"},
}http.HandleFunc("/welcome", func(w http.ResponseWriter, r *http.Request) {
renderer.Render(w, "welcome.tmpl", nil)
})
http.ListenAndServe(":4000")
}
```## Included Functions
HTML templates will include two functions from the start: `dict` and `list`.
- `dict` given a list of key-value pairs, it will construct a map `map[string]any` out of it.
Example:```
{{$_ := dict "foo" "bar"}} => map[string]any{"foo": "bar"}
```It's very usefull for passing named parameters to other templates, for example:
```handlebars
{{define "greet"}}
Hello, {{.Name}}!
{{end}}{{template "greet" dict
"Name" "john"
}}
```- `list`: given a list of items, it will construct an slice `[]any` out of it.
Example:```
{{$_ := list "foo" "bar"}} => []any{"foo", "bar"}
```