Dynamic FAQ page template
This commit is contained in:
parent
13bf91ba7d
commit
920e7972af
@ -18,3 +18,31 @@ func StaticTemplate(templatePath string) http.HandlerFunc {
|
|||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) }
|
return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FAQ(templatePath string) http.HandlerFunc {
|
||||||
|
questions := []struct {
|
||||||
|
Question string
|
||||||
|
Answer string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Question: "Is this a real website?",
|
||||||
|
Answer: "No.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Question: "I Can Has Cheezburger?",
|
||||||
|
Answer: "No.",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl := views.Must(views.FromFS(templates.FS, templatePath))
|
||||||
|
|
||||||
|
var testWriter strings.Builder
|
||||||
|
err := tpl.ExecuteWriter(&testWriter, nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tpl.Execute(w, questions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@ -20,7 +20,7 @@ func main() {
|
|||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Get("/", ctrlrs.StaticTemplate("home.gohtml"))
|
r.Get("/", ctrlrs.StaticTemplate("home.gohtml"))
|
||||||
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml"))
|
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml"))
|
||||||
r.Get("/faq", ctrlrs.StaticTemplate("faq.gohtml"))
|
r.Get("/faq", ctrlrs.FAQ("faq.gohtml"))
|
||||||
r.NotFound(notFoundHandler)
|
r.NotFound(notFoundHandler)
|
||||||
fmt.Println("Starting the server on :3000...")
|
fmt.Println("Starting the server on :3000...")
|
||||||
http.ListenAndServe(":3000", r)
|
http.ListenAndServe(":3000", r)
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
<h1>FAQ</h1>
|
<h1>FAQ</h1>
|
||||||
<hr>
|
<hr>
|
||||||
<h3>Is this a real website?</h3>
|
{{range .}}
|
||||||
<p>No.</p>
|
{{template "qa" .}}
|
||||||
<h3>I Can Has Cheezburger?</h3>
|
{{end}}
|
||||||
<p>No.</p>
|
|
||||||
|
{{define "qa"}}
|
||||||
|
<h3>{{.Question}}</h3>
|
||||||
|
<p>{{.Answer}}</p>
|
||||||
|
{{end}}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user