Search code examples
gotemplate-enginego-html-template

How to not escape in HTML templates


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.


Solution

  • 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