12

Trying to render HTML templates for sending via email with embedded attachments with cid:. Problem is, that Go does escaping and I cannot do anything.

tplVars := map[string]interface{}{
    "Dog": "cid:dog.png",
    "Cat": "cid:cat.png",
}

My testing template looks more less like this:

Dog: <img src="{{.Dog}}">
Cat: {{.Cat}}

Output is:

Dog: <img src="#ZgotmplZ">
Cat: cid:cat.png

If text is outside attribute context, it is evaluated correctly, but when it is an src attribute it always become that error string. I tried also change value from string to template.HTMLAttr but nothing happen. Cid value is always evaluated to that error output #ZgotmplZ.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Arxeiss
  • 976
  • 16
  • 34
  • Possible duplicate of [How to get rid of ZgotmplZ from html/template in golang?](http://stackoverflow.com/questions/36382624/how-to-get-rid-of-zgotmplz-from-html-template-in-golang) and [Go: unescape css input in HTML](http://stackoverflow.com/questions/27906812/go-unescape-css-input-in-html) – icza Apr 29 '16 at 07:30

1 Answers1

17

The issue is that the src attribute isn't treated strictly as an attribute, but as a URL. If you change it from a string to a template.URL it works just fine.

tplVars := map[string]interface{}{
    "Dog": template.URL("cid:dog.png"),
    "Cat": "cid:cat.png",
}

https://play.golang.org/p/ZN27nGnUE9

dave
  • 62,300
  • 5
  • 72
  • 93
  • Thanks a lot.. I'm totally blind. I tried all possible types as HTML, HTMLAttr, even CSS etc. But I didn't see URL type and the end of index. Thank you – Arxeiss Apr 29 '16 at 07:28
  • Arrrgh this _ought_ to be on the documentation **somewhere**. The hours I've lost trying to figure it out on my own!! Thanks for posting an answer, though. It was _really_ helpful (even 6 years later!) – Gwyneth Llewelyn Mar 21 '22 at 20:14