The following program writes
<html><body>Hello <script>[{"A":"foo","B":"bar"},{"A":"bar","B":"baz"}]</script></body></html>
because of the <script>
-Tag (which does some JavaScript JSON to string encoding). How can I get the same without the <script>
-Tag?. That is: I'd like to write
t, err := template.New("foo").Parse("<html><body>Hello <pre>{{.}}</pre></body></html>
")
and get
<html><body>Hello <pre>[{"A":"foo","B":"bar"},{"A":"bar","B":"baz"}]</pre></body></html>
back? I have seen the | ...
syntax for contexts in the templates package, but which context should I use?
package main
import (
"html/template"
"log"
"os"
)
func main() {
type keyvalue struct {
A, B string
}
a := []keyvalue{{"foo", "bar"}, {"bar", "baz"}}
t, err := template.New("foo").Parse("<html><body>Hello <script>{{.}}</script></body></html>
")
if err != nil {
log.Fatal(err)
}
err = t.ExecuteTemplate(os.Stdout, "foo", a)
if err != nil {
log.Fatal(err)
}
}
Background: I need to generate an HTML attribute for the X-Editable JavaScript library, which looks like this: source="[{value: 1, text: 'text1'}, {value: 2, text: 'text2'}, ...]"