Advertisement
arxeiss

Template parsing

May 16th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.95 KB | None | 0 0
  1. var imgAttachments := regexp.MustCompile("\\{\\{([\\s-]*img )")
  2.  
  3. func replaceImgTagsInHTML(imgs []*data.Attachment, name string) (html_template.URL, error) {
  4.     for i := range imgs {
  5.         if imgs[i].Name == name {
  6.             return html_template.URL(imgs[i].Path), nil
  7.         }
  8.     }
  9.     return "", fmt.Sprintf("Image attachment %v is not found", name)
  10. }
  11.  
  12. func ParseTemplate(template string, attachments []*data.Attachment, recipient *data.Recipient, sender *data.Sender) (string, error) {  
  13.     tplVars := map[string]interface{}{
  14.         "Recipient":     recipient,
  15.         "Sender":        sender,
  16.         "ImgAttachment": attachments,
  17.     }
  18.     content := imgAttachments.ReplaceAllString(template, "{{$1.ImgAttachment ")
  19.     parser, err := html_template.New("Email").Funcs(html_template.FuncMap{
  20.         "img": replaceImgTagsInHTML,
  21.     }).Parse(content)
  22.     if err == nil {
  23.         wr := bytes.NewBufferString("")
  24.         err = parser.Execute(wr, tplVars)
  25.     }
  26.     if err != nil{
  27.         return "", err
  28.     }
  29.     return wr.String(), nil
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement