Skip to content
/ tswap Public

Package tswap automatically updates an html/template.Template when files in the directory where the template definitions are stored are updated.

License

Notifications You must be signed in to change notification settings

cdillond/tswap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tswap

Go Reference
Package tswap automatically updates an html/template.Template when files in the directory where the template definitions are stored are updated. This can be useful if you want to work on changes to a website's UI without recompiling your application every time minor updates to a template definition file are made. tswap is dependent on github.com/fsnotify/fsnotify, which works for most, but not all, commonly used OS's.

Example

package main

import (
	"fmt"
	"html/template"
	"net/http"
	"sync"

	"github.com/cdillond/tswap"
)

type App struct {
	Mux *http.ServeMux
    	Rwm sync.RWMutex
	T   *template.Template
}

func main() {
	a := App{
		Mux: http.NewServeMux(),
        	Rwm: sync.RWMutex{},
	}
	dir := `templates/`
	t, err := template.ParseGlob(dir + `*`)
	if err != nil {
		panic(err)
	}
	a.T = t

	errChan := tswap.AutoUpdate(a.T, dir, &a.Rwm)
	go func() {
		for {
			err = <-errChan
			fmt.Println(err)
		}
	}()

	a.Mux.HandleFunc("/", a.Index)

	http.ListenAndServe(":1234", a.Mux)
}

func (a *App) Index(w http.ResponseWriter, r *http.Request) {
	a.Rwm.RLock()
	t, err := a.T.Lookup(`index.html`).Clone()
    	a.Rwm.RUnlock()
	if err != nil {
		return
	}
	t.Execute(w, struct{}{})
}

About

Package tswap automatically updates an html/template.Template when files in the directory where the template definitions are stored are updated.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages