Golang html/template.HTMLAttr type example

package html/template

HTMLAttr encapsulates an HTML attribute from a trusted source, for example, dir="ltr".

Golang html/template.HTMLAttr type usage example

 type navItem struct {
 Name  string
 Attrs map[template.HTMLAttr]string
 }

 func activeNav(active string) []navItem {
  // create menu items
  about := navItem{
 Name: "About",
 Attrs: map[template.HTMLAttr]string{
 "href":  "/about",
 "title": "About Page",
 },
  }
  home := navItem{
 Name: "Home",
 Attrs: map[template.HTMLAttr]string{
 "href":  "/",
 "title": "Home Page",
 },
  }
  // set active menu class
  switch active {
  case "about":
 about.Attrs["class"] = "active"
  case "home":
 home.Attrs["class"] = "active"
  }
  return []navItem{home, about}
 }

References :

http://golang.org/pkg/html/template/#HTMLAttr

https://github.com/james-maloney/templates/blob/master/example/main.go

Advertisement