-1

I have some data given to my template using Go that contains html tags. When I try to display this data, it displays the full text literally, including the HTML tags. Here's the code:

In my .tmpl, I am looping through an object called .SimRpms whose {{ $value }} has html in it like <br>:

{{ range $key, $value := .SimRpms }}
    <div class="col-md-4">
        <div class="panel panel-default">
          <div class="panel-heading">{{ $key }}</div>
          <div class="panel-body">{{ $value }}</div>
        </div>
    </div>
{{ end }}

However when I look at the rendered page, the {{ $value }} is rendered as plain text instead of html:

html is rendered as plain text

How can I get {{ $value }} to interpret the text as HTML?

Here's what it looks like in the JS Console:

js console

I know this is similar to this question: Golang html output is being interpreted as plain text instead of being received as html however the rest of the page is displaying HTML ok and the content type of the entire page has been set already, but this little panel isn't displaying the HTML (it's displaying it literally).

Katie
  • 45,622
  • 19
  • 93
  • 125
  • This is explained in the Introduction section of the documentation for `html/template`: https://golang.org/pkg/html/template/ – Adrian Aug 17 '18 at 13:50

2 Answers2

3
package main

import (
    "html/template"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", SayHello)
    log.Fatal(http.ListenAndServe(":2332", nil))
}
func SayHello(w http.ResponseWriter, r *http.Request) {
    t, err := template.ParseGlob("hello.gtml")
    if err != nil {
        panic(err)
    }
    t.Execute(w, map[string]template.HTML{"name": template.HTML("<a>Katie Sissons</a>")})
    return
}

works with the template

hello.gtml below.

<html>
    <head></head>
    <body>{{ .name  }}</body>
</html>

html/template package treats template.HTML as a type and applies escape to normal strings. If you want your string to go without escape, this type can do that for you.

whitespace
  • 789
  • 6
  • 13
0

What I ended up doing to get this to work was manipulating the data that I am displaying. Instead of using map[string]string for SimRpms, I used map[string][]string so that i can iterate through the string slice and insert a <br> between them:

{{ range $key, $value := .SimRpms }}
    <div class="col-md-4">
        <div class="panel panel-default">
          <div class="panel-heading">{{ $key }}</div>
          <div class="panel-body">
            {{ range $keytwo, $valuetwo := $value }}
                {{ $valuetwo }}<br>
            {{ end }}
          </div>
        </div>
    </div>
{{ end }}
Katie
  • 45,622
  • 19
  • 93
  • 125
  • 1
    if you change `map[string][]string` to `map[string][]template.HTML`, you might get the result you are looking for. – whitespace Aug 18 '18 at 03:06
  • @whitespace oh! That would've been an easier solution :( thank you for the info! I'll use it next time I run into this – Katie Aug 20 '18 at 17:16