Created
April 16, 2022 03:47
-
-
Save emctague/86ec89492ac40318b9b5999d18917b9f to your computer and use it in GitHub Desktop.
Golang Template Abstraction for Debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TplRunner interface { | |
Execute(w io.Writer, data any) error | |
} | |
type DebugTplRunner string | |
func (r DebugTplRunner) Execute(w io.Writer, data any) error { | |
t, err := template.ParseFiles(string(r)) | |
if err != nil { | |
return err | |
} | |
return t.Execute(w, data) | |
} | |
// Now, both template.Template and DebugTplRunner can be used as type TplRunner. | |
// This allows for switching on/off automatic template reloading, e.g.: | |
// | |
// var tpl TplRunner | |
// if debug { | |
// tpl = DebugTplRunner("index.html") | |
// } else { | |
// tpl = template.Must(template.ParseFiles("index.html")) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment