Skip to content

Instantly share code, notes, and snippets.

@lswest
Last active May 13, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lswest/feed0fb8685b0d9bed03e864a78f7f1a to your computer and use it in GitHub Desktop.
Save lswest/feed0fb8685b0d9bed03e864a78f7f1a to your computer and use it in GitHub Desktop.
Example Go Project for FCM 121
{{ define "header" }}
<header>
<nav>
<a href="#">Example</a>
<a href="#">Example</a>
</nav>
</header>
{{ end }}
package main
import (
"net/http"
"strings"
"html/template"
)
type Page struct {
Title string
Content string
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
hello := Page{"Hello World", "This is a hello world page"}
if(len(r.URL.Path) > len("/")) {
person := r.URL.Path[len("/"):]
hello = Page{"Hello Person", "Hello, "+person}
}
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
}
tpl := template.Must(template.New("hello.html").Funcs(funcMap).ParseGlob("tmpl/*"))
tpl.ExecuteTemplate(w, "hello.html", hello)
}
func main() {
http.HandleFunc("/", viewHandler)
http.ListenAndServe(":8081", nil)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ .Title | html}}</title>
</head>
<body>
{{ template "header" }}
<h1>{{ .Title | html }}</h1>
<p>all lowercase: {{ .Title | ToLower }}</p>
<main>
<p>{{ .Content | html }}</p>
</main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment