Guest User

Untitled

a guest
Apr 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. // 1. MarshalHTML is a method for the wfs3.ConformanceClasses struct instead of func RenderConformanceHTML().
  2. // 2. map[string]string instead of custom data type for template params.
  3. // 3. Keep config out of template.
  4.  
  5. var tmpl_conformance = `<!doctype html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="utf-8">
  9. <title>{{ .title }}</title>
  10. </head>
  11. <body>
  12. <h1>{{ .title }}</h1>
  13. <h2>Conformance</h2>
  14. <ul>
  15. {{ .content }}
  16. </ul>
  17. </body>
  18. </html>`
  19.  
  20. func (ccs *wfs3.ConformanceClasses) MarshalHTML(c *config.Config /* Better just to pass title? */) []byte {
  21. var rslt bytes.Buffer
  22.  
  23. t := template.New("conformance")
  24. t, _ = t.Parse(tmpl_conformance)
  25.  
  26. // This is pretty silly for ConformanceClasses, but for other types the content value
  27. // would be constructed via recursive calls to <nested_type>.MarshalHTML()
  28. content := ""
  29. for _, ct := range ccs.ConformsTo {
  30. content = append(fmt.Sprintf(" <li>%v</li>\n", ct))
  31. }
  32.  
  33. // Since we're ultimately making a string, let's use built-in map instead of custom data-passing
  34. // types.
  35. data := make(map[string]string)
  36. data["content"] = c
  37. // This also removes a direct coupling of the templates from the config. If something
  38. // breaks when the config type is changed, it will show up on compile instead of quietly
  39. // rendering an empty string in the template.
  40. data["title"] = c.Metadata.Identification.Title
  41. if err := t.Execute(&rslt, data); err != nil {
  42. return rslt.Bytes(), err
  43. }
  44.  
  45. // FIXME: should be a better way
  46. return rslt.Bytes(), nil
  47. }
Add Comment
Please, Sign In to add comment