Skip to content

Commit

Permalink
cleaned up and added an example how to use HTML templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Feb 27, 2023
1 parent 3963e02 commit 22e9bfc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
39 changes: 10 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,38 @@
package main

import (
"errors"
"fmt"
"net/http"
"text/template"
)

const portNumber = ":8080"

// Home is the handler for the home page
func Home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is the home page")
rendernTemplate(w, "home-page.tpml")
}

// About is the handler for the about page
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))
}

// Divide divides one value into another and returns message with result
func Divide(w http.ResponseWriter, r *http.Request) {
x := 100.0
y := 0.0
f, err := divideValues(x, y)
if err != nil {
_, _ = fmt.Fprintf(w, "Error: Division by zero is not a valid operation. Error returned: %s", err)
return
}
rendernTemplate(w, "about-page.tpml")

_, _ = fmt.Fprintf(w, fmt.Sprintf("%f divided by %f is %f", x, y, f))
}

// divideValues divides two floats x and y, and returns the quotient and a value of type error
func divideValues(x, y float64) (float64, error) {
if y == 0 {
err := errors.New("Divisor is zero!")
return 0, err
// rendernTemplate serves as a wrapper and renders
// a template from folder /templates to a desired writer
func rendernTemplate(w http.ResponseWriter, tpml string) {
parsedTemplate, _ := template.ParseFiles("./templates/" + tpml)
err := parsedTemplate.Execute(w, nil)
if err != nil {
fmt.Println("error parsing template:", err)
}
result := x / y
return result, nil
}

// addValues adds two ints x and y, and returns the sum
func addValues(x, y int) int {
return x + y
}

// main is the main function
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/about", About)
http.HandleFunc("/divide", Divide)

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
_ = http.ListenAndServe(portNumber, nil)
Expand Down
12 changes: 12 additions & 0 deletions templates/about-page.tpml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>This is the about page</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions templates/home-page.tpml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>This is the home page</h1>
</body>
</html>

0 comments on commit 22e9bfc

Please sign in to comment.