Examples of code I've written in PHP, Javascript, SCSS, etc.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

view.go 1.6 KiB

il y a 3 ans
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package view
  2. import (
  3. "html/template"
  4. "net/http"
  5. "log"
  6. )
  7. var (
  8. available = true
  9. matrix = "zgvhdgh0b3nwawv6"
  10. )
  11. type Page struct {
  12. tpl *template.Template
  13. Title string
  14. Name string
  15. Matrix string
  16. Available bool
  17. }
  18. var titles = map[string]string {
  19. "home": "Home",
  20. "services": "Services",
  21. "contact": "Contact",
  22. "web": "Web Development",
  23. "projects": "Projects",
  24. "about": "About",
  25. }
  26. var paths = map[string]string {
  27. "home": "view/home.tpl",
  28. "services": "view/services.tpl",
  29. "contact": "view/contact.tpl",
  30. "web": "view/web.tpl",
  31. "projects": "view/projects.tpl",
  32. "about": "view/about.tpl",
  33. }
  34. var pages = map[string]Page {
  35. "home": cache("home"),
  36. "services": cache("services"),
  37. "contact": cache("contact"),
  38. "web": cache("web"),
  39. "projects": cache("projects"),
  40. "about": cache("about"),
  41. }
  42. func parse(path string) *template.Template {
  43. var paths = []string{"view/master.tpl", path}
  44. tpl, err := template.ParseFiles(paths...)
  45. if err != nil {
  46. log.Print(err, "| Could not parse:", path)
  47. }
  48. return tpl
  49. }
  50. func Create(name string) Page {
  51. tpl := parse(paths[name])
  52. title := titles[name]
  53. return Page{tpl: tpl,
  54. Title: title,
  55. Name: name,
  56. Matrix: matrix,
  57. Available: available}
  58. }
  59. func cache(name string) Page {
  60. var p = []string{"view/master.tpl", paths[name]}
  61. tpl := template.Must(template.ParseFiles(p...))
  62. return Page{tpl: tpl,
  63. Title: titles[name],
  64. Name: name,
  65. Matrix: matrix,
  66. Available: available}
  67. }
  68. func Get(name string) Page {
  69. return pages[name]
  70. }
  71. func (page Page) Render(w http.ResponseWriter) {
  72. err := page.tpl.Execute(w, page)
  73. if err != nil {
  74. log.Print(err)
  75. }
  76. }