动态处理模板映射的值

huangapple go评论93阅读模式
英文:

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{
		&quot;common&quot;: &quot;Some GPM consistency checkers have failed.&quot;,
		&quot;t_0&quot;: &quot;Title1&quot;,
		&quot;t_1&quot;: &quot;Title2&quot;,
		&quot;v_0&quot;: &quot;V1&quot;,
		&quot;v_1&quot;: &quot;V2&quot;,

I want to print it in table like:

Title1 Title2
V1 V2

I started something like:

{{ range .Annotations.SortedPairs }}
  {{ if match &quot;^t_&quot; .Name }}
    &lt;p&gt;{{ .Value }}&lt;/p&gt;
  {{ 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(&quot;hello.gohtml&quot;)
	if err != nil {
		panic(err)
	}

	http.HandleFunc(&quot;/&quot;, handler)
	http.ListenAndServe(&quot;:3000&quot;, 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{
			&quot;common&quot;: &quot;Some GPM consistency checkers have failed.&quot;,
			&quot;t_0&quot;:    &quot;Title1&quot;,
			&quot;t_1&quot;:    &quot;Title2&quot;,
			&quot;v_0&quot;:    &quot;V1&quot;,
			&quot;v_1&quot;:    &quot;V2&quot;,
		},
	}
	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, &quot;t_&quot;, &quot;&quot;, 1)
	for key, val := range ann {
		if key == &quot;v_&quot;+code {
			return val
		}
	}
	return &quot;&quot;
}

then you can use this function in your template like this:

{{ range $key, $value := .Annotations.SortedPairs }}
	   	{{ if match &quot;^t_&quot; .$key }}
	   		&lt;p&gt;{{FindValue &quot;.$key&quot;}}&lt;/p&gt;
	   	{{ end  }}
	   	{{ end }}

I hope this helps to you. if there is any problem tell me because I didn't test this code.

huangapple
  • 本文由 发表于 2022年4月28日 15:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/72039844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定