From a64a03232426dec0a7759efa87657c0bdd08232d Mon Sep 17 00:00:00 2001 From: b3t0 Date: Wed, 17 Apr 2024 14:24:03 -0600 Subject: [PATCH] handlers and render funcs take apart --- handlers.go | 15 +++++++++++++++ main.go | 20 -------------------- render.go | 16 ++++++++++++++++ templates/about.page.tmpl | 11 ++++++++++- templates/home.page.tmpl | 7 +++++++ 5 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 handlers.go create mode 100644 render.go diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..d22cac3 --- /dev/null +++ b/handlers.go @@ -0,0 +1,15 @@ +package main + +import ( + "net/http" +) + +// Home is the home page handler +func Home(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, "home.page.tmpl") +} + +// About is the about page handler +func About(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, "about.page.tmpl") +} diff --git a/main.go b/main.go index a840042..627c073 100644 --- a/main.go +++ b/main.go @@ -3,30 +3,10 @@ package main import ( "fmt" "net/http" - "text/template" ) var portNumber = ":8080" -// Home is the home page handler -func Home(w http.ResponseWriter, r *http.Request) { - renderTemplate(w, "home.page.tmpl") -} - -// About is the about page handler -func About(w http.ResponseWriter, r *http.Request) { - renderTemplate(w, "about.page.tmpl") -} - -func renderTemplate(w http.ResponseWriter, tmpl string) { - parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl) - err := parsedTemplate.Execute(w, nil) - if err != nil { - fmt.Println("error parsing template:", err) - return - } -} - // main is the main application function func main() { http.HandleFunc("/", Home) diff --git a/render.go b/render.go new file mode 100644 index 0000000..e208804 --- /dev/null +++ b/render.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "net/http" + "text/template" +) + +func renderTemplate(w http.ResponseWriter, tmpl string) { + parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl) + err := parsedTemplate.Execute(w, nil) + if err != nil { + fmt.Println("error parsing template:", err) + return + } +} diff --git a/templates/about.page.tmpl b/templates/about.page.tmpl index 422657c..c485d3c 100644 --- a/templates/about.page.tmpl +++ b/templates/about.page.tmpl @@ -4,8 +4,17 @@ Document + + -

This is de about page

+
+
+
+

This is de about page

+

This is some text

+
+
+
diff --git a/templates/home.page.tmpl b/templates/home.page.tmpl index cf1bf19..68c9e55 100644 --- a/templates/home.page.tmpl +++ b/templates/home.page.tmpl @@ -4,8 +4,15 @@ Document + +
+
+

This is de home page

+
+
+