动态处理模板映射的值

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

Dynamically process template map values

问题

我有一个像这样的注释对象:

  1. 注释:map[string]string{
  2. "common": "一些 GPM 一致性检查器失败。",
  3. "t_0": "标题1",
  4. "t_1": "标题2",
  5. "v_0": "V1",
  6. "v_1": "V2",
  7. }

我想以表格形式打印出来,如下所示:

标题1 标题2
V1 V2

我已经开始了一些工作,像这样:

  1. {{ range .Annotations.SortedPairs }}
  2. {{ if match "^t_" .Name }}
  3. <p>{{ .Value }}</p>
  4. {{ end }}
  5. {{ end }}

但我无法真正弄清楚应该如何完成。我希望能够动态地完成,因为列的数量可能不同。

英文:

I have annotations object like this:

  1. Annotations: map[string]string{
  2. &quot;common&quot;: &quot;Some GPM consistency checkers have failed.&quot;,
  3. &quot;t_0&quot;: &quot;Title1&quot;,
  4. &quot;t_1&quot;: &quot;Title2&quot;,
  5. &quot;v_0&quot;: &quot;V1&quot;,
  6. &quot;v_1&quot;: &quot;V2&quot;,

I want to print it in table like:

Title1 Title2
V1 V2

I started something like:

  1. {{ range .Annotations.SortedPairs }}
  2. {{ if match &quot;^t_&quot; .Name }}
  3. &lt;p&gt;{{ .Value }}&lt;/p&gt;
  4. {{ end }}
  5. {{ 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模板中添加一个辅助函数。

  1. var testTemplate *template.Template
  2. func main() {
  3. var err error
  4. testTemplate, err = template.ParseFiles("hello.gohtml")
  5. if err != nil {
  6. panic(err)
  7. }
  8. http.HandleFunc("/", handler)
  9. http.ListenAndServe(":3000", nil)
  10. }
  1. func handler(w http.ResponseWriter, r *http.Request) {
  2. data := struct {
  3. FindValue func(ann map[string]string, needle string) string
  4. Annotations map[string]string
  5. }{
  6. FindValue,
  7. map[string]string{
  8. "common": "Some GPM consistency checkers have failed.",
  9. "t_0": "Title1",
  10. "t_1": "Title2",
  11. "v_0": "V1",
  12. "v_1": "V2",
  13. },
  14. }
  15. err := testTemplate.Execute(w, data)
  16. if err != nil {
  17. http.Error(w, err.Error(), http.StatusInternalServerError)
  18. }
  19. }
  1. func FindValue(ann map[string]string, needle string) string {
  2. var code = strings.Replace(needle, "t_", "", 1)
  3. for key, val := range ann {
  4. if key == "v_"+code {
  5. return val
  6. }
  7. }
  8. return ""
  9. }

然后你可以在你的模板中像这样使用这个函数:

  1. {{ range $key, $value := .Annotations.SortedPairs }}
  2. {{ if match "^t_" $key }}
  3. <p>{{FindValue $key}}</p>
  4. {{ end }}
  5. {{ end }}

希望这对你有所帮助。如果有任何问题,请告诉我,因为我没有测试过这段代码。

英文:

I think you should add a helper function to your Go template.

  1. var testTemplate *template.Template
  2. func main() {
  3. var err error
  4. testTemplate, err = template.ParseFiles(&quot;hello.gohtml&quot;)
  5. if err != nil {
  6. panic(err)
  7. }
  8. http.HandleFunc(&quot;/&quot;, handler)
  9. http.ListenAndServe(&quot;:3000&quot;, nil)
  10. }
  1. func handler(w http.ResponseWriter, r *http.Request) {
  2. data := struct {
  3. FindValue func(ann map[string]string, needle string) string
  4. Annotations map[string]string
  5. }{
  6. FindValue,
  7. map[string]string{
  8. &quot;common&quot;: &quot;Some GPM consistency checkers have failed.&quot;,
  9. &quot;t_0&quot;: &quot;Title1&quot;,
  10. &quot;t_1&quot;: &quot;Title2&quot;,
  11. &quot;v_0&quot;: &quot;V1&quot;,
  12. &quot;v_1&quot;: &quot;V2&quot;,
  13. },
  14. }
  15. err := testTemplate.Execute(w, data)
  16. if err != nil {
  17. http.Error(w, err.Error(), http.StatusInternalServerError)
  18. }
  19. }
  1. func FindValue(ann map[string]string, needle string) string {
  2. var code = strings.Replace(needle, &quot;t_&quot;, &quot;&quot;, 1)
  3. for key, val := range ann {
  4. if key == &quot;v_&quot;+code {
  5. return val
  6. }
  7. }
  8. return &quot;&quot;
  9. }

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

  1. {{ range $key, $value := .Annotations.SortedPairs }}
  2. {{ if match &quot;^t_&quot; .$key }}
  3. &lt;p&gt;{{FindValue &quot;.$key&quot;}}&lt;/p&gt;
  4. {{ end }}
  5. {{ 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:

确定