I am having a problem to properly escape javascript urls in my templates. I 
do have the situation where I build a template that is having javascript 
urls that are from variables in the go program (read from yaml files). The 
go program generates static html, but the html is supposed to use 
moustache.js to expand some further variables at render time. I am just not 
able to preserve my javascript from the html/template escaping. Any ideas 
what I am doing wrong? 

The output is:

<a href="javascript:doSlide%28%27%7b%7barea%7d%7d%27%29;">{{test}}</a>


But I would like it to be:

<a href="javascript:doSlide('{{area}}');">{{test}}</a>


package main

import (
"html/template"
"os"
)

var t = template.Must(template.New("test").Funcs(template.FuncMap{
"safeattr": func(value string) template.HTMLAttr {
return template.HTMLAttr(value)
},
"safehtml": func(value string) template.HTML {
return template.HTML(value)
},
"safejs": func(value string) template.JS {
return template.JS(value)
},
"safecss": func(value string) template.CSS {
return template.CSS(value)
},
"safeurl": func(value string) template.URL {
return template.URL(value)
},
}).Parse(`
<a href="{{safeurl .href}}">{{safehtml .content}}</a>
`))

func main() {
data := map[string]string{
"href":    "javascript:doSlide('{{area}}');",
"content": "{{test}}",
}
err := t.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}



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


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/0c93e49c-1e8b-4617-baef-0848b57fde93%40googlegroups.com.

Reply via email to