I finally got a workaround going, and that is to not have any moustache 
template in the javascript at all. By putting the {{area}} template in an 
data-xxx attribute of the <a> element I can access this from the javascript.

On Monday, September 9, 2019 at 1:53:34 PM UTC+2, Jens-Uwe Mager wrote:
>
> 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/865e2df0-db52-4dd8-8c84-43c2cc56c599%40googlegroups.com.

Reply via email to