Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. MarshalHTML is a method for the wfs3.ConformanceClasses struct instead of func RenderConformanceHTML().
- // 2. map[string]string instead of custom data type for template params.
- // 3. Keep config out of template.
- var tmpl_conformance = `<!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>{{ .title }}</title>
- </head>
- <body>
- <h1>{{ .title }}</h1>
- <h2>Conformance</h2>
- <ul>
- {{ .content }}
- </ul>
- </body>
- </html>`
- func (ccs *wfs3.ConformanceClasses) MarshalHTML(c *config.Config /* Better just to pass title? */) []byte {
- var rslt bytes.Buffer
- t := template.New("conformance")
- t, _ = t.Parse(tmpl_conformance)
- // This is pretty silly for ConformanceClasses, but for other types the content value
- // would be constructed via recursive calls to <nested_type>.MarshalHTML()
- content := ""
- for _, ct := range ccs.ConformsTo {
- content = append(fmt.Sprintf(" <li>%v</li>\n", ct))
- }
- // Since we're ultimately making a string, let's use built-in map instead of custom data-passing
- // types.
- data := make(map[string]string)
- data["content"] = c
- // This also removes a direct coupling of the templates from the config. If something
- // breaks when the config type is changed, it will show up on compile instead of quietly
- // rendering an empty string in the template.
- data["title"] = c.Metadata.Identification.Title
- if err := t.Execute(&rslt, data); err != nil {
- return rslt.Bytes(), err
- }
- // FIXME: should be a better way
- return rslt.Bytes(), nil
- }
Add Comment
Please, Sign In to add comment