seperate templating for routes
This commit is contained in:
parent
c0b4e92a57
commit
34fe9b4992
@ -1,7 +1,6 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,14 +9,6 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
Title: "Example Page",
|
Title: "Example Page",
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("web/views/#layout.html", "web/views/example.html", "web/views/#header.html", "web/views/#footer.html")
|
// Call renderTemplate with the specific template for this route
|
||||||
if err != nil {
|
renderTemplate(w, data, "web/views/example.html")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,14 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PageData struct {
|
|
||||||
Title string
|
|
||||||
}
|
|
||||||
|
|
||||||
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
func RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
data := PageData{
|
data := PageData{
|
||||||
Title: "Home Page",
|
Title: "Home Page",
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("web/views/#layout.html", "web/views/index.html", "web/views/#header.html", "web/views/#footer.html")
|
// Call renderTemplate with the specific template for this route
|
||||||
if err != nil {
|
renderTemplate(w, data, "web/views/index.html")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
35
src/routes/template.go
Normal file
35
src/routes/template.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PageData struct {
|
||||||
|
Title string
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderTemplate(w http.ResponseWriter, data PageData, specificTemplate string) {
|
||||||
|
// List of common templates
|
||||||
|
commonTemplates := []string{
|
||||||
|
"web/views/#layout.html",
|
||||||
|
"web/views/#header.html",
|
||||||
|
"web/views/#footer.html",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the specific template for the route
|
||||||
|
templates := append(commonTemplates, specificTemplate)
|
||||||
|
|
||||||
|
// Parse all templates
|
||||||
|
tmpl, err := template.ParseFiles(templates...)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the "layout" template
|
||||||
|
err = tmpl.ExecuteTemplate(w, "layout", data)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user