From 54d3976f90716ce381509af2624e4df0f434a2f0 Mon Sep 17 00:00:00 2001 From: b3t0 Date: Tue, 16 Apr 2024 12:16:17 -0600 Subject: [PATCH] render html --- main.go | 18 +++++++++++------- templates/about.page.tmpl | 11 +++++++++++ templates/home.page.tmpl | 11 +++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 templates/about.page.tmpl create mode 100644 templates/home.page.tmpl diff --git a/main.go b/main.go index cc00b66..a840042 100644 --- a/main.go +++ b/main.go @@ -3,24 +3,28 @@ 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) { - fmt.Fprintf(w, "This is the home page") + renderTemplate(w, "home.page.tmpl") } // About is the about page handler func About(w http.ResponseWriter, r *http.Request) { - sum := addValues(2, 2) - _, _ = fmt.Fprintf(w, fmt.Sprintf("This is the about page and 2 + 2 is %d", sum)) + renderTemplate(w, "about.page.tmpl") } -// addValues adds twu integers and return the sum -func addValues(x, y int) int { - return x + y +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 @@ -28,6 +32,6 @@ func main() { http.HandleFunc("/", Home) http.HandleFunc("/about", About) - fmt.Println(fmt.Sprintf("Staritn application on port %s", portNumber)) + fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber)) _ = http.ListenAndServe(portNumber, nil) } diff --git a/templates/about.page.tmpl b/templates/about.page.tmpl new file mode 100644 index 0000000..422657c --- /dev/null +++ b/templates/about.page.tmpl @@ -0,0 +1,11 @@ + + + + + + Document + + +

This is de about page

+ + diff --git a/templates/home.page.tmpl b/templates/home.page.tmpl new file mode 100644 index 0000000..cf1bf19 --- /dev/null +++ b/templates/home.page.tmpl @@ -0,0 +1,11 @@ + + + + + + Document + + +

This is de home page

+ +