英文:
Dynamically process template map values
问题
我有一个像这样的注释对象:
注释:map[string]string{
"common": "一些 GPM 一致性检查器失败。",
"t_0": "标题1",
"t_1": "标题2",
"v_0": "V1",
"v_1": "V2",
}
我想以表格形式打印出来,如下所示:
标题1 | 标题2 |
---|---|
V1 | V2 |
我已经开始了一些工作,像这样:
{{ range .Annotations.SortedPairs }}
{{ if match "^t_" .Name }}
<p>{{ .Value }}</p>
{{ end }}
{{ end }}
但我无法真正弄清楚应该如何完成。我希望能够动态地完成,因为列的数量可能不同。
英文:
I have annotations object like this:
Annotations: map[string]string{
"common": "Some GPM consistency checkers have failed.",
"t_0": "Title1",
"t_1": "Title2",
"v_0": "V1",
"v_1": "V2",
I want to print it in table like:
Title1 | Title2 |
---|---|
V1 | V2 |
I started something like:
{{ range .Annotations.SortedPairs }}
{{ if match "^t_" .Name }}
<p>{{ .Value }}</p>
{{ end }}
{{ end }}
but can't really figure out how it should be done. I want to do it dynamically because there can be different number of columns
答案1
得分: 1
我认为你应该在你的Go模板中添加一个辅助函数。
var testTemplate *template.Template
func main() {
var err error
testTemplate, err = template.ParseFiles("hello.gohtml")
if err != nil {
panic(err)
}
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
data := struct {
FindValue func(ann map[string]string, needle string) string
Annotations map[string]string
}{
FindValue,
map[string]string{
"common": "Some GPM consistency checkers have failed.",
"t_0": "Title1",
"t_1": "Title2",
"v_0": "V1",
"v_1": "V2",
},
}
err := testTemplate.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func FindValue(ann map[string]string, needle string) string {
var code = strings.Replace(needle, "t_", "", 1)
for key, val := range ann {
if key == "v_"+code {
return val
}
}
return ""
}
然后你可以在你的模板中像这样使用这个函数:
{{ range $key, $value := .Annotations.SortedPairs }}
{{ if match "^t_" $key }}
<p>{{FindValue $key}}</p>
{{ end }}
{{ end }}
希望这对你有所帮助。如果有任何问题,请告诉我,因为我没有测试过这段代码。
英文:
I think you should add a helper function to your Go template.
var testTemplate *template.Template
func main() {
var err error
testTemplate, err = template.ParseFiles("hello.gohtml")
if err != nil {
panic(err)
}
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
data := struct {
FindValue func(ann map[string]string, needle string) string
Annotations map[string]string
}{
FindValue,
map[string]string{
"common": "Some GPM consistency checkers have failed.",
"t_0": "Title1",
"t_1": "Title2",
"v_0": "V1",
"v_1": "V2",
},
}
err := testTemplate.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func FindValue(ann map[string]string, needle string) string {
var code = strings.Replace(needle, "t_", "", 1)
for key, val := range ann {
if key == "v_"+code {
return val
}
}
return ""
}
then you can use this function in your template like this:
{{ range $key, $value := .Annotations.SortedPairs }}
{{ if match "^t_" .$key }}
<p>{{FindValue ".$key"}}</p>
{{ end }}
{{ end }}
I hope this helps to you. if there is any problem tell me because I didn't test this code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论