-
-
Save mkmik/7692221 to your computer and use it in GitHub Desktop.
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
<p>Hello</p> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="content"> | |
{{template "content" .}} | |
</div> | |
</body> | |
</html> |
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
package main | |
import ( | |
"log" | |
"net/http" | |
"text/template" | |
) | |
var ( | |
layout = template.Must(template.ParseFiles("layout.html")) | |
index = parsePartial(layout, "index.html") | |
) | |
// parsePartial parses a partial template file and adds it as a template named 'content' | |
// to a clone of the layout template. | |
func parsePartial(layout *template.Template, name string) *template.Template { | |
t := template.Must(template.ParseFiles(name)) | |
l := template.Must(layout.Clone()) | |
template.Must(l.AddParseTree("content", t.Tree)) | |
return l | |
} | |
// MainHandler is an example handler | |
func MainHandler(w http.ResponseWriter, r *http.Request) { | |
if err := index.Execute(w, nil); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", MainHandler) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="content"> | |
<p>Hello</p> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment