https://github.com/heppu/embdr
Go template embedding tool
https://github.com/heppu/embdr
build-tool code-generator embedded go golang html-template static-assets templates
Last synced: about 2 months ago
JSON representation
Go template embedding tool
- Host: GitHub
- URL: https://github.com/heppu/embdr
- Owner: heppu
- License: mit
- Created: 2020-01-24T06:08:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-28T14:28:06.000Z (over 6 years ago)
- Last Synced: 2025-10-29T15:39:00.277Z (9 months ago)
- Topics: build-tool, code-generator, embedded, go, golang, html-template, static-assets, templates
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Embdr
[](https://pkg.go.dev//github.com/heppu/embdr)
[](https://goreportcard.com/report/github.com/heppu/embdr)
[](https://codecov.io/gh/heppu/embdr)
[](https://github.com/heppu/embdr/releases)
[](https://github.com/heppu/embdr/issues)
[](https://github.com/heppu/embdr/LICENSE)
Emdeb Go templates with zero 3rd party dependencies.
Just generate and load.
Generate:
```
embdr -p mypkg -o templates.go index.tmpl`
```
Load:
```go
tmpl, err := LoadTemplate("index.tmpl")
```
## Install
go get github.com/heppu/embdr/cmd/embdr
## Usage
```
embdr -p [-o output_file] [file_1 file_2 ...]
Flags:
-p package name
-o output file (default STDOUT)
```
### Embed single file
```bash
embdr -p mypkg -o templates.go index.tmpl
```
### Embed all files from folder
```bash
find ./templates/ -name '*.tmpl' | ./embdr -p mypkg -o templates.go
```
### Using go generate directive
Define template file.
```
{{- /* user.tmpl */ -}}
Hi there {{.Name}}!
```
Then create file that has the //go:generate directive.
```go
// main.go
package main
//go:generate embdr -p main -o templates.go user.tmpl
import (
"log"
"os"
"text/template"
)
type User struct {
Name string
}
func main() {
const name = "user.tmpl"
data, err := LoadTemplate(name)
if err != nil {
log.Fatal(err)
}
t, err := template.New(name).Parse(data)
if err != nil {
log.Fatal(err)
}
err = t.Execute(os.Stdout, User{Name: "Gopher"})
if err != nil {
log.Fatal(err)
}
}
```
Now just generate embedded templates with go generate and you are ready to go!
```bash
$ go generate ./...
$ go run main.go templates.go
Hi there Gopher!
```
## Why one more embedding tool?
There are almost as many static asset embedding tools for go as there are http routers. Why I decided to make one more was to have something extremely simple with no additional knobs and pulls. Just give the input, ouput and package name and don't care about anything else. I also didn't want force users to use any external 3rd party dependencies.