handlers and render funcs take apart
This commit is contained in:
15
handlers.go
Normal file
15
handlers.go
Normal file
@@ -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")
|
||||
}
|
||||
20
main.go
20
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)
|
||||
|
||||
16
render.go
Normal file
16
render.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,17 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is de about page</h1>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h1>This is de about page</h1>
|
||||
<p>This is some text</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -4,8 +4,15 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h1>This is de home page</h1>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user