diff --git a/httputils/templates.go b/httputils/templates.go index c4e8eb2..7c48e1e 100644 --- a/httputils/templates.go +++ b/httputils/templates.go @@ -13,7 +13,7 @@ import ( type TemplateSet struct { templates *template.Template paths []string - modTimes map[string]time.Time + loadTimes map[string]time.Time } func newTemplateSet(partials *TemplateSet, paths ...string) TemplateSet { @@ -33,7 +33,7 @@ func newTemplateSet(partials *TemplateSet, paths ...string) TemplateSet { return TemplateSet{ templates: templates, paths: allPaths, - modTimes: modTimes, + loadTimes: modTimes, } } @@ -49,19 +49,21 @@ func (templateSet *TemplateSet) ExecuteTemplate(wr io.Writer, name string, data return templateSet.templates.ExecuteTemplate(wr, name, data) } -func (templateSet *TemplateSet) reloadTemplateIfModified(path string) { - fileInfo, _ := os.Stat(path) - modTime := fileInfo.ModTime() - if modTime.After(templateSet.modTimes[path]) { - fmt.Printf("Reloading template %s...\n", path) - templateSet.templates.ParseFiles(path) - templateSet.modTimes[path] = modTime - } -} - func (templateSet *TemplateSet) reloadTemplatesIfModified() { + for path, loadTime := range templateSet.loadTimes { + fileInfo, _ := os.Stat(path) + modTime := fileInfo.ModTime() + if modTime.After(loadTime) { + fmt.Printf("%s updated: reloading templates...\n", path) + goto update + } + } + return +update: + now := time.Now() for _, path := range templateSet.paths { - templateSet.reloadTemplateIfModified(path) + templateSet.templates.ParseFiles(path) + templateSet.loadTimes[path] = now } }