A simple page for all your links.
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.
 
 
 
 

40 lines
924 B

  1. package publicbio
  2. import (
  3. "html/template"
  4. "io"
  5. "log"
  6. "net/http"
  7. )
  8. var templates = map[string]*template.Template{}
  9. const templatesDir = "templates/"
  10. func init() {
  11. initTemplate("profile")
  12. }
  13. func initTemplate(name string) {
  14. templates[name] = template.Must(template.New(name).ParseFiles(templatesDir+name+".tmpl", templatesDir+"base.tmpl"))
  15. }
  16. // renderTemplate retrieves the given template and renders it to the given io.Writer.
  17. // If something goes wrong, the error is logged and returned.
  18. func renderTemplate(w io.Writer, tmpl string, data interface{}) error {
  19. err := templates[tmpl].ExecuteTemplate(w, tmpl, data)
  20. if err != nil {
  21. log.Printf("[ERROR] Error rendering %s: %s\n", tmpl, err)
  22. }
  23. return err
  24. }
  25. func (app *app) pageHandler(name string) http.HandlerFunc {
  26. return func(w http.ResponseWriter, r *http.Request) {
  27. handleError(w, r, func() error {
  28. return renderTemplate(w, name, nil)
  29. }())
  30. }
  31. }