Skip to content

heppu/embdr

Repository files navigation

Embdr

Documentation Go Report Card codecov Release GitHub issues license

Emdeb Go templates with zero 3rd party dependencies.

Just generate and load.

Generate:

embdr -p mypkg -o templates.go index.tmpl`

Load:

tmpl, err := LoadTemplate("index.tmpl")

Install

go get github.com/heppu/embdr/cmd/embdr

Usage

embdr -p <package_name> [-o output_file] [file_1 file_2 ...]

Flags:

	-p  package name
	-o  output file (default STDOUT)

Embed single file

embdr -p mypkg -o templates.go index.tmpl

Embed all files from folder

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.

// 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!

$ 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.