Add executeTemplate function
This commit is contained in:
parent
4e2ad80fdf
commit
aac5e45b3f
24
main.go
24
main.go
@ -9,9 +9,9 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
func executeTemplate(w http.ResponseWriter, filepath string) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||||
tpl, err := template.ParseFiles("templates/home.gohtml")
|
tpl, err := template.ParseFiles(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error parsing template: %v", err)
|
log.Printf("Error parsing template: %v", err)
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
@ -24,20 +24,14 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := "templates/home.gohtml"
|
||||||
|
executeTemplate(w, path)
|
||||||
|
}
|
||||||
func contactHandler(w http.ResponseWriter, r *http.Request) {
|
func contactHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
path := "templates/contact.gohtml"
|
||||||
tpl, err := template.ParseFiles("templates/contact.gohtml")
|
executeTemplate(w, path)
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error parsing template: %v", err)
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = tpl.Execute(w, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error executing template: %v", err)
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
func faqHandler(w http.ResponseWriter, r *http.Request) {
|
func faqHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user