mirror of
https://github.com/mainnika/nikita-tokarch-uk.git
synced 2026-05-25 01:03:35 +00:00
Implement template funcs controller instead of simple funcs map
This commit is contained in:
@@ -52,7 +52,8 @@ func main() {
|
|||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = templates.Load()
|
templateFuncs := &templates.Funcs{Version: Version}
|
||||||
|
err = templates.Load(templateFuncs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-18
@@ -1,24 +1,66 @@
|
|||||||
package templates
|
package templates
|
||||||
|
|
||||||
import "html/template"
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/url"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
// UseFuncs returns a func map with template helpers functions
|
type Funcs struct {
|
||||||
func UseFuncs() template.FuncMap {
|
Version string
|
||||||
|
|
||||||
|
compiledJSAppURL string
|
||||||
|
|
||||||
|
initOnce sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Funcs) init() {
|
||||||
|
|
||||||
|
jsAppURL, err := url.Parse(URLJSApp)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
q := jsAppURL.Query()
|
||||||
|
q.Add("version", f.Version)
|
||||||
|
|
||||||
|
jsAppURL.RawQuery = q.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
f.compiledJSAppURL = jsAppURL.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Funcs) add(i int) int {
|
||||||
|
return i + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Funcs) sub(i int) int {
|
||||||
|
return i - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Funcs) getJSAppURL() string {
|
||||||
|
|
||||||
|
f.initOnce.Do(f.init)
|
||||||
|
|
||||||
|
return f.compiledJSAppURL
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Funcs) getIndexURL() string {
|
||||||
|
return URLIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Funcs) getBlogURL() string {
|
||||||
|
return URLBlog
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use returns a func map with template helpers functions
|
||||||
|
func (f *Funcs) Use() template.FuncMap {
|
||||||
return template.FuncMap{
|
return template.FuncMap{
|
||||||
"add": func(i int) int {
|
"add": f.add,
|
||||||
return i + 1
|
"sub": f.sub,
|
||||||
},
|
"getJSAppURL": f.getJSAppURL,
|
||||||
"sub": func(i int) int {
|
"getIndexURL": f.getIndexURL,
|
||||||
return i - 1
|
"getBlogURL": f.getBlogURL,
|
||||||
},
|
|
||||||
"getJSAppURL": func() string {
|
|
||||||
return URLJSApp
|
|
||||||
},
|
|
||||||
"getIndexURL": func() string {
|
|
||||||
return URLIndex
|
|
||||||
},
|
|
||||||
"getBlogURL": func() string {
|
|
||||||
return URLBlog
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,12 @@ import (
|
|||||||
var content embed.FS
|
var content embed.FS
|
||||||
|
|
||||||
// List of compiled go-templates
|
// List of compiled go-templates
|
||||||
var Templates *template.Template
|
var Templates *template.Template = template.New("")
|
||||||
|
|
||||||
// Load embeded templates
|
// Load embeded templates
|
||||||
func Load() (err error) {
|
func Load(funcs *Funcs) (err error) {
|
||||||
|
|
||||||
Templates = template.New("")
|
Templates.Funcs(funcs.Use())
|
||||||
Templates.Funcs(UseFuncs())
|
|
||||||
|
|
||||||
tmplNames, err := fs.Glob(content, "*.go.tmpl")
|
tmplNames, err := fs.Glob(content, "*.go.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user