Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var imgAttachments := regexp.MustCompile("\\{\\{([\\s-]*img )")
- func replaceImgTagsInHTML(imgs []*data.Attachment, name string) (html_template.URL, error) {
- for i := range imgs {
- if imgs[i].Name == name {
- return html_template.URL(imgs[i].Path), nil
- }
- }
- return "", fmt.Sprintf("Image attachment %v is not found", name)
- }
- func ParseTemplate(template string, attachments []*data.Attachment, recipient *data.Recipient, sender *data.Sender) (string, error) {
- tplVars := map[string]interface{}{
- "Recipient": recipient,
- "Sender": sender,
- "ImgAttachment": attachments,
- }
- content := imgAttachments.ReplaceAllString(template, "{{$1.ImgAttachment ")
- parser, err := html_template.New("Email").Funcs(html_template.FuncMap{
- "img": replaceImgTagsInHTML,
- }).Parse(content)
- if err == nil {
- wr := bytes.NewBufferString("")
- err = parser.Execute(wr, tplVars)
- }
- if err != nil{
- return "", err
- }
- return wr.String(), nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement