All of my templates have a variable indicating the root url of their associated images. I want to output that root before the filename of the image in the body of the template, but when I do so, the templates package attempts to put quotes around it. Here's a minimal piece of code that shows my problem. IMG_ROOT is an interface in this example to better simulate the real code. The script type is text/template because its contents will be used in an underscore.js template. The type doesn't appear to matter to how it's output though.
package main
import (
"html/template"
"os"
)
type Data struct {
IMG_ROOT interface{}
}
const tmpl = `
<body>
<script type="text/template">
<img src="{{.IMG_ROOT}}/file_name.jpg"></img>
</script>
</body>
`
func main() {
t, _ := template.New("").Parse(tmpl)
t.Execute( os.Stdout, &Data{ template.JSStr( "/path/to/file" ) } )
}
http://play.golang.org/p/wg9JK2w2lt
If I write <img src="{{.IMG_ROOT}}/file_name.jpg"></img>
, I get "\/path\/to\/file/file_name.jpg"
which isn't valid.
If I write <img src={{.IMG_ROOT}}/file_name.jpg></img>
, I get "/path/to/file/"file_name.jpg
which doesn't work either.
Is there a workaround for this? I've tried using every 'safe' type from the templates package for the IMG_ROOT type, but haven't had any luck. I could just write a function that concatenates and returns the root and file, but it seems like there should be a simpler solution.
Any ideas?
Thanks!