Yes, it is possible to reload (html or text) templates at runtime.
As long as the folder that you are reading the templates from is accessible by the application you can simply repeat the template parsing.
You could do something like this:
var templates = template.New("Template")
func folderChanged(folder string) bool {
// todo: implement filesystem watcher here
return true
}
func ReloadTemplates(templateFolder string) {
newTemplates, err := LoadTemplates(templateFolder)
if err != nil {
log.Println("Unable to load templates: %s", err)
return
}
// override existing templates variable
templates = newTemplates
}
func LoadTemplates(folder string) (*template.Template, error) {
template := template.New("Template")
walkError := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".html") {
_, parseError := template.ParseFiles(path)
if parseError != nil {
return parseError
}
}
return nil
})
return template, walkError
}
func Render(w http.ResponseWriter, tmplName string, data interface{}) {
templateFolder := "view"
if folderChanged(templateFolder) {
ReloadTemplates(templateFolder)
}
if err := templates.ExecuteTemplate(w, tmplName, data); err != nil {
log.Println(err)
}
}
If you are using a relative path like the ./view
-folder from your example it will only work if the directory from which you are executing the application has such a sub-folder. If that is not the case I would suggest using a path relative to the users' home directory or something like that.