Escaping 'to' in golang html template
How to prevent escaping '
before '
in html template:
package main
import (
"html/template"
"os"
)
const tmpl = `<html>
<head>
<title>{{.Title}}</title>
</head>
</html>`
func main() {
t := template.Must(template.New("ex").Parse(tmpl))
v := map[string]interface{}{
"Title": template.HTML("Hello World'"),
}
t.Execute(os.Stdout, v)
}
It outputs:
<html>
<head>
<title>Hello World'</title>
</head>
</html>
Desired output:
<html>
<head>
<title>Hello World'</title>
</head>
</html>
+3
source to share
1 answer
@dyoo made it clear that content is <title>
treated as RCDATA. The code that does the escaping is here . In the branch if t == contentTypeHTML
, what happens is what happens with template.HTML
.
If you really need to control the source output, use text/template
and do manual escaping.
+1
source to share