mirror of https://github.com/jlelse/GoBlog
Rework template initialization, load all templates from template directory
This commit is contained in:
parent
bf28e34038
commit
9846cf2211
6
main.go
6
main.go
|
@ -25,7 +25,11 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
initMarkdown()
|
initMarkdown()
|
||||||
initRendering() // Needs assets
|
err = initRendering() // Needs assets
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare graceful shutdown
|
// Prepare graceful shutdown
|
||||||
quit := make(chan os.Signal, 1)
|
quit := make(chan os.Signal, 1)
|
||||||
|
|
36
render.go
36
render.go
|
@ -2,26 +2,31 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const templatesDir = "templates"
|
||||||
|
const templatesExt = ".gohtml"
|
||||||
|
|
||||||
|
const templateBase = "base"
|
||||||
const templatePost = "post"
|
const templatePost = "post"
|
||||||
const templateError = "error"
|
const templateError = "error"
|
||||||
const templateRedirect = "redirect"
|
const templateRedirect = "redirect"
|
||||||
const templateIndex = "index"
|
const templateIndex = "index"
|
||||||
const templateSummary = "summary"
|
|
||||||
const templateTaxonomy = "taxonomy"
|
const templateTaxonomy = "taxonomy"
|
||||||
const templateMenu = "menu"
|
|
||||||
|
|
||||||
var templates map[string]*template.Template
|
var templates map[string]*template.Template
|
||||||
var templateFunctions template.FuncMap
|
var templateFunctions template.FuncMap
|
||||||
|
|
||||||
func initRendering() {
|
func initRendering() error {
|
||||||
templateFunctions = template.FuncMap{
|
templateFunctions = template.FuncMap{
|
||||||
"blog": func() *configBlog {
|
"blog": func() *configBlog {
|
||||||
return appConfig.Blog
|
return appConfig.Blog
|
||||||
|
@ -69,13 +74,24 @@ func initRendering() {
|
||||||
}
|
}
|
||||||
|
|
||||||
templates = make(map[string]*template.Template)
|
templates = make(map[string]*template.Template)
|
||||||
for _, name := range []string{templatePost, templateError, templateRedirect, templateIndex, templateSummary, templateTaxonomy, templateMenu} {
|
|
||||||
templates[name] = loadTemplate(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadTemplate(name string) *template.Template {
|
baseTemplatePath := path.Join(templatesDir, templateBase+templatesExt)
|
||||||
return template.Must(template.New(name).Funcs(templateFunctions).ParseFiles("templates/base.gohtml", fmt.Sprintf("templates/%s.gohtml", name)))
|
err := filepath.Walk(templatesDir, func(p string, info os.FileInfo, err error) error {
|
||||||
|
if info.Mode().IsRegular() && path.Ext(p) == templatesExt {
|
||||||
|
name := strings.TrimSuffix(path.Base(p), templatesExt)
|
||||||
|
if name != templateBase {
|
||||||
|
templates[name], err = template.New(name).Funcs(templateFunctions).ParseFiles(baseTemplatePath, p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func render(w http.ResponseWriter, template string, data interface{}) {
|
func render(w http.ResponseWriter, template string, data interface{}) {
|
||||||
|
|
Loading…
Reference in New Issue