You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
858 B

  1. package Webserver
  2. import (
  3. "html/template"
  4. "net/http"
  5. "path"
  6. "PersonalWebsite/Helper"
  7. )
  8. type Unauthenticated struct {
  9. FlashMsg string
  10. }
  11. var (
  12. partials = []string{
  13. "html/header.gohtml",
  14. "html/sidebar.gohtml",
  15. }
  16. )
  17. func ServeTemplate(w http.ResponseWriter, r *http.Request, mainFile string, v map[string]interface{}) {
  18. var (
  19. tpl *template.Template
  20. files []string
  21. e error
  22. )
  23. v["test"] = "Yeet"
  24. files = []string{webRootJoin(mainFile)}
  25. for _, p := range partials {
  26. files = append(files, webRootJoin(p))
  27. }
  28. tpl, e = template.New(path.Base(files[0])).Funcs(
  29. template.FuncMap{
  30. "FormatTimestamp": Helper.FormatTimestamp,
  31. },
  32. ).ParseFiles(files...)
  33. if e != nil {
  34. // TODO: Handle this
  35. panic(e)
  36. }
  37. w.Header().Set("Content-type", "text/html")
  38. e = tpl.Execute(w, v)
  39. if e != nil {
  40. // TODO: Handle this
  41. panic(e)
  42. }
  43. }