update render method

This commit is contained in:
kenneth 2023-12-01 02:52:56 +00:00
parent 1bb57bc94a
commit 26c1f6aa72
4 changed files with 25 additions and 8 deletions

View File

@ -39,5 +39,5 @@ func (server *Server) home(w http.ResponseWriter, r *http.Request) {
} }
func renderHome(w http.ResponseWriter, data any) { func renderHome(w http.ResponseWriter, data any) {
render(w, data, "web/templates/home.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/home.html.tmpl")
} }

View File

@ -6,7 +6,24 @@ import (
"net/http" "net/http"
) )
func render(w http.ResponseWriter, data any, tmpls ...string) { // func render(w http.ResponseWriter, data any, tmpls ...string) {
// t, err := template.ParseFiles(tmpls...)
// if err != nil {
// log.Printf("template parse: %v", err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// err = t.Execute(w, data)
// if err != nil {
// log.Printf("template execute: %v", err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// }
func renderLayout(w http.ResponseWriter, data any, tmpls ...string) {
tmpls = append(tmpls, "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl")
t, err := template.ParseFiles(tmpls...) t, err := template.ParseFiles(tmpls...)
if err != nil { if err != nil {
log.Printf("template parse: %v", err) log.Printf("template parse: %v", err)

View File

@ -17,7 +17,7 @@ func (server *Server) registerView(w http.ResponseWriter, r *http.Request) {
} }
func renderRegister(w http.ResponseWriter, data any) { func renderRegister(w http.ResponseWriter, data any) {
render(w, data, "web/templates/user/register.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/user/register.html.tmpl")
} }
// type userResponse struct { // type userResponse struct {
@ -127,7 +127,7 @@ func (server *Server) loginView(w http.ResponseWriter, r *http.Request) {
} }
func renderLogin(w http.ResponseWriter, data any) { func renderLogin(w http.ResponseWriter, data any) {
render(w, data, "web/templates/user/login.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/user/login.html.tmpl")
} }
// type loginUserResponse struct { // type loginUserResponse struct {

View File

@ -32,7 +32,7 @@ func (server *Server) play(w http.ResponseWriter, r *http.Request) {
if err == nil { if err == nil {
data.Authorize = *auth data.Authorize = *auth
} }
render(w, data, "web/templates/video/play.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/video/play.html.tmpl")
} }
/* /*
@ -111,7 +111,7 @@ func (server *Server) videosView(w http.ResponseWriter, r *http.Request) {
} }
} }
render(w, data, "web/templates/me/videos.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/me/videos.html.tmpl")
} }
func (server *Server) createVideoView(w http.ResponseWriter, r *http.Request) { func (server *Server) createVideoView(w http.ResponseWriter, r *http.Request) {
@ -134,7 +134,7 @@ func (server *Server) createVideoView(w http.ResponseWriter, r *http.Request) {
} }
func renderCreateVideo(w http.ResponseWriter, data any) { func renderCreateVideo(w http.ResponseWriter, data any) {
render(w, data, "web/templates/video/edit.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/video/edit.html.tmpl")
} }
type videoCreateResp struct { type videoCreateResp struct {
@ -261,7 +261,7 @@ func (server *Server) transferView(w http.ResponseWriter, r *http.Request) {
if err == nil { if err == nil {
data.Authorize = *u data.Authorize = *u
} }
render(w, data, "web/templates/video/transfer.html.tmpl", "web/templates/base/header.html.tmpl", "web/templates/base/footer.html.tmpl") renderLayout(w, data, "web/templates/video/transfer.html.tmpl")
} }
func (server *Server) transfer(w http.ResponseWriter, r *http.Request) { func (server *Server) transfer(w http.ResponseWriter, r *http.Request) {