Created
June 18, 2019 13:21
-
-
Save leafnode/c50bba1dbfb98b14406929fd29a8ce21 to your computer and use it in GitHub Desktop.
Template helper function for Go #go #golang
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
func executeTemplate(tmpls *template.Template, tmplName string, w io.Writer, data interface{}) error { | |
var err error | |
layout := tmpls.Lookup("layout.html") | |
if layout == nil { | |
return errNoLayout | |
} | |
layout, err = layout.Clone() | |
if err != nil { | |
return err | |
} | |
t := tmpls.Lookup(tmplName) | |
if t == nil { | |
return errNoTemplate | |
} | |
_, err = layout.AddParseTree("content", t.Tree) | |
if err != nil { | |
return err | |
} | |
return layout.Execute(w, data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment