Advertisement
arxeiss

Templates in Go

Apr 28th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.65 KB | None | 0 0
  1. func init() {
  2.    
  3.     funcMap = template.FuncMap{
  4.         "img": func(imgs []*templateAttachment, name string) (string, error) {
  5.             for i := range imgs {
  6.                 if imgs[i].Name == name {
  7.                     return imgs[i].Path, nil
  8.                 }
  9.             }
  10.             return "", &AppError{
  11.                 Action: "FuncMap",
  12.                 Unit:   "Template",
  13.                 Text:   fmt.Sprintf("Image attachment %v is not found", name),
  14.             }
  15.         },
  16.     }
  17. }
  18.  
  19. func ParseTemplate(.....){
  20.    ......
  21.    templateParser, err := template.New("Email").Funcs(funcMap).Parse(content)
  22.     if err != nil {
  23.         return "", err
  24.     }
  25.     wr := bytes.NewBufferString("")
  26.     err = templateParser.Execute(wr, tplVars)
  27.     if err != nil {
  28.         return "", err
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement