Skip to content

Commit

Permalink
update render method
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth committed Dec 1, 2023
1 parent 1bb57bc commit 26c1f6a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/handlers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func (server *Server) home(w http.ResponseWriter, r *http.Request) {
}

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")
}
19 changes: 18 additions & 1 deletion internal/handlers/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@ import (
"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...)
if err != nil {
log.Printf("template parse: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (server *Server) registerView(w http.ResponseWriter, r *http.Request) {
}

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 {
Expand Down Expand Up @@ -127,7 +127,7 @@ func (server *Server) loginView(w http.ResponseWriter, r *http.Request) {
}

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 {
Expand Down
8 changes: 4 additions & 4 deletions internal/handlers/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (server *Server) play(w http.ResponseWriter, r *http.Request) {
if err == nil {
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")
}

/*
Expand Down Expand Up @@ -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) {
Expand All @@ -134,7 +134,7 @@ func (server *Server) createVideoView(w http.ResponseWriter, r *http.Request) {
}

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 {
Expand Down Expand Up @@ -261,7 +261,7 @@ func (server *Server) transferView(w http.ResponseWriter, r *http.Request) {
if err == nil {
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) {
Expand Down

0 comments on commit 26c1f6a

Please sign in to comment.