Pagina utenti caricata da json
This commit is contained in:
+25
-16
@@ -3,6 +3,7 @@ package templates
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
@@ -10,20 +11,23 @@ import (
|
||||
"git.phc.dm.unipi.it/phc/website/util"
|
||||
)
|
||||
|
||||
// CachedTemplate holds a reference to the list of patterns to preload and a reference to the already parsed template
|
||||
type CachedTemplate struct {
|
||||
LoadPatterns []string
|
||||
cachedTmpl *template.Template
|
||||
PreLoadTemplatePatterns []string
|
||||
cachedTmpl *template.Template
|
||||
}
|
||||
|
||||
// NewCacheTemplate creates a new template from a list of patterns a loads it
|
||||
func NewCacheTemplate(loadPatterns ...string) *CachedTemplate {
|
||||
cachedTemplate := &CachedTemplate{
|
||||
LoadPatterns: loadPatterns,
|
||||
cachedTmpl: nil,
|
||||
PreLoadTemplatePatterns: loadPatterns,
|
||||
cachedTmpl: nil,
|
||||
}
|
||||
template.Must(cachedTemplate.Load(template.New("")))
|
||||
return cachedTemplate
|
||||
}
|
||||
|
||||
// Load returns the cached template or loads it from disk using the list of preload (glob) patterns.
|
||||
func (ct *CachedTemplate) Load(t *template.Template) (*template.Template, error) {
|
||||
if ct.cachedTmpl != nil {
|
||||
return ct.cachedTmpl, nil
|
||||
@@ -31,7 +35,7 @@ func (ct *CachedTemplate) Load(t *template.Template) (*template.Template, error)
|
||||
|
||||
ct.cachedTmpl = t
|
||||
|
||||
for _, pattern := range ct.LoadPatterns {
|
||||
for _, pattern := range ct.PreLoadTemplatePatterns {
|
||||
var err error
|
||||
ct.cachedTmpl, err = ct.cachedTmpl.ParseGlob(pattern)
|
||||
if err != nil {
|
||||
@@ -42,38 +46,43 @@ func (ct *CachedTemplate) Load(t *template.Template) (*template.Template, error)
|
||||
return ct.cachedTmpl, nil
|
||||
}
|
||||
|
||||
// Reload the pattern from disk (used for developing)
|
||||
func (ct *CachedTemplate) Reload() {
|
||||
ct.cachedTmpl = nil
|
||||
template.Must(ct.Load(nil))
|
||||
}
|
||||
|
||||
// Template returns the cached template
|
||||
func (ct *CachedTemplate) Template() *template.Template {
|
||||
log.Printf("preloadPatterns: %v", ct.PreLoadTemplatePatterns)
|
||||
return ct.cachedTmpl
|
||||
}
|
||||
|
||||
// Renderer holds cached templates for rendering
|
||||
type Renderer struct {
|
||||
rootPath string
|
||||
loadPatterns []string
|
||||
templateCache map[string]*CachedTemplate
|
||||
// TODO: Add a render function to CachedTemplate instead of returning the template reference. In this way the Template Renderer would not have a "direct" dependency on "html/template"...
|
||||
|
||||
// TemplateRenderer holds cached templates for rendering
|
||||
type TemplateRenderer struct {
|
||||
viewsDir string
|
||||
preloadTemplatePatterns []string
|
||||
templateCache map[string]*CachedTemplate
|
||||
}
|
||||
|
||||
// NewRenderer constructs a template renderer with a base file
|
||||
func NewRenderer(rootPath string, loadPatterns ...string) *Renderer {
|
||||
return &Renderer{
|
||||
func NewRenderer(rootPath string, loadPatterns ...string) *TemplateRenderer {
|
||||
return &TemplateRenderer{
|
||||
rootPath,
|
||||
loadPatterns,
|
||||
map[string]*CachedTemplate{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Renderer) Load(name string) *CachedTemplate {
|
||||
func (r *TemplateRenderer) Load(name string) *CachedTemplate {
|
||||
cachedTemplate, present := r.templateCache[name]
|
||||
|
||||
if !present {
|
||||
loaders := []string{}
|
||||
loaders = append(loaders, r.loadPatterns...)
|
||||
loaders = append(loaders, path.Join(r.rootPath, name))
|
||||
loaders = append(loaders, r.preloadTemplatePatterns...)
|
||||
loaders = append(loaders, path.Join(r.viewsDir, name))
|
||||
cachedTemplate = NewCacheTemplate(loaders...)
|
||||
r.templateCache[name] = cachedTemplate
|
||||
}
|
||||
@@ -82,7 +91,7 @@ func (r *Renderer) Load(name string) *CachedTemplate {
|
||||
}
|
||||
|
||||
// Render the template, also injects "Page" and "Config" values in the template
|
||||
func (r *Renderer) Render(w io.Writer, name string, data util.H) error {
|
||||
func (r *TemplateRenderer) Render(w io.Writer, name string, data util.H) error {
|
||||
cachedTemplate := r.Load(name)
|
||||
|
||||
if config.Mode == "development" {
|
||||
|
||||
Reference in New Issue
Block a user