Static site generator for making web mixtapes in 2020.

render.go 887B

12345678910111213141516171819202122232425262728293031323334353637
  1. //go:generate inline -o templates.go -p cdr mixtape.tmpl templates/parts.tmpl
  2. package cdr
  3. import (
  4. "html/template"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. )
  9. func Render(m *Mixtape, w io.Writer) error {
  10. partsRawTmpl, err := ReadAsset("templates/parts.tmpl", false)
  11. if err != nil {
  12. return err
  13. }
  14. mixtapeRawTmpl, err := ioutil.ReadFile("mixtape.tmpl")
  15. if err != nil {
  16. log.Print("Unable to load local mixtape.tmpl; falling back to default")
  17. mixtapeRawTmpl, err = ReadAsset("mixtape.tmpl", false)
  18. if err != nil {
  19. return err
  20. }
  21. } else {
  22. log.Print("Generating from local mixtape.tmpl")
  23. }
  24. t, err := template.New("mixtape").Parse(string(mixtapeRawTmpl) + string(partsRawTmpl))
  25. if err != nil {
  26. log.Printf("[ERROR] Unable to parse: %v", err)
  27. return err
  28. }
  29. err = t.ExecuteTemplate(w, "mixtape", m)
  30. if err != nil {
  31. log.Printf("[ERROR] Unable to render: %v", err)
  32. }
  33. return nil
  34. }