parent
3f64c8e928
commit
554bba9ff1
After Width: | Height: | Size: 234 KiB |
@ -1,24 +1,3 @@
|
||||
package main
|
||||
|
||||
import "html/template"
|
||||
|
||||
var (
|
||||
tmpl *template.Template
|
||||
templates = map[string]string{
|
||||
"index": "res/index.html",
|
||||
"setting": "res/setting.html",
|
||||
}
|
||||
)
|
||||
|
||||
func ParseTemplate(name string, content string) {
|
||||
if tmpl == nil {
|
||||
tmpl = template.New(name)
|
||||
}
|
||||
var t *template.Template
|
||||
if tmpl.Name() == name {
|
||||
t = tmpl
|
||||
} else {
|
||||
t = tmpl.New(name)
|
||||
}
|
||||
template.Must(t.New(name).Delims("[[", "]]").Parse(content))
|
||||
}
|
||||
var templateDir = "res"
|
||||
|
@ -0,0 +1,42 @@
|
||||
// +build bindata
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var tmpl *template.Template
|
||||
|
||||
func parseTemplate(name string, content string) {
|
||||
if tmpl == nil {
|
||||
tmpl = template.New(name)
|
||||
}
|
||||
var t *template.Template
|
||||
if tmpl.Name() == name {
|
||||
t = tmpl
|
||||
} else {
|
||||
t = tmpl.New(name)
|
||||
}
|
||||
template.Must(t.New(name).Delims("[[", "]]").Parse(content))
|
||||
}
|
||||
|
||||
func init() {
|
||||
http.Handle("/res/", http.StripPrefix("/res/", http.FileServer(assetFS())))
|
||||
}
|
||||
|
||||
func executeTemplate(wr io.Writer, name string, data interface{}) {
|
||||
if tmpl == nil || tmpl.Lookup(name) == nil {
|
||||
path := filepath.Join(templateDir, name+".html")
|
||||
data, err := Asset(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
parseTemplate(name, string(data))
|
||||
}
|
||||
tmpl.ExecuteTemplate(wr, name, data)
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// +build !bindata
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func init() {
|
||||
fs := http.FileServer(http.Dir(templateDir))
|
||||
http.Handle("/res/", http.StripPrefix("/res/", fs))
|
||||
}
|
||||
|
||||
func executeTemplate(wr io.Writer, name string, data interface{}) {
|
||||
path := filepath.Join(templateDir, name+".html")
|
||||
body, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
template.Must(template.New("t").Delims("[[", "]]").Parse(string(body))).Execute(wr, data)
|
||||
}
|
Loading…
Reference in new issue