2 回答

TA貢獻1836條經驗 獲得超5個贊
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
}
使用模板
hello.gtml以下。
<html>
<head></head>
<body>{{ .name }}</body>
</html>
html/template包將其視為template.HTML一種類型并將轉義應用于普通字符串。如果你想讓你的字符串不轉義,這種類型可以為你做到這一點。

TA貢獻1836條經驗 獲得超3個贊
為了使它起作用,我最終所做的是操縱我正在顯示的數據。我沒有使用map[string]stringfor ,而是使用了這樣我可以遍歷字符串切片并在它們之間插入一個:SimRpmsmap[string][]string<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">
{{ range $keytwo, $valuetwo := $value }}
{{ $valuetwo }}<br>
{{ end }}
</div>
</div>
</div>
{{ end }}
- 2 回答
- 0 關注
- 244 瀏覽
添加回答
舉報