Reputation: 2298
Although I guess it could be a bug in Go template, and filed an issue:
https://github.com/golang/go/issues/38895
I would like to ask here, the "by design" behavior of Go's html/template, especially it's rule with <script>
tag. See below:
package main
import (
"fmt"
"html/template"
"os"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
u := User{1, "xrfang"}
//without <script>
t, _ := template.New("body").Parse("var u = {{.}}")
t.Execute(os.Stdout, u)
fmt.Println()
//with <script>
t, _ = template.New("body").Parse("<script>/*var u1 = {{.}}*/ var u2 = {{.}}</script>")
t.Execute(os.Stdout, u)
fmt.Println()
}
The template without <script>
generated string same as output of go's %+v
fmt string. But the <script>
version generated JSON, and the code in-between /* ... */
is automatically removed??
Upvotes: 0
Views: 293
Reputation: 42413
The template without generated string same as output of go's %+v fmt string. But the version generated JSON, and the code in-between /* ... */ is automatically removed??
Yes. That is intentional. Escaping in html/template is context specific as it must be context specific to be safe as explained in the package doc.
Upvotes: 3