Dynamic FAQ page template

This commit is contained in:
Lucas Schumacher 2024-08-01 10:11:56 -04:00
parent 13bf91ba7d
commit 920e7972af
3 changed files with 37 additions and 5 deletions

View File

@ -18,3 +18,31 @@ func StaticTemplate(templatePath string) http.HandlerFunc {
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)
}
}

View File

@ -20,7 +20,7 @@ func main() {
r.Use(middleware.Logger)
r.Get("/", ctrlrs.StaticTemplate("home.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)
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)

View File

@ -1,6 +1,10 @@
<h1>FAQ</h1>
<hr>
<h3>Is this a real website?</h3>
<p>No.</p>
<h3>I Can Has Cheezburger?</h3>
<p>No.</p>
{{range .}}
{{template "qa" .}}
{{end}}
{{define "qa"}}
<h3>{{.Question}}</h3>
<p>{{.Answer}}</p>
{{end}}