golang template with multiple structs

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

golang template with multiple structs

问题

我有一个包含JSON字段的结构体,类似于这样:

detail := &Detail{
Name string
Detail json.RawMessage
}

模板看起来像这样:

detail = 在{{Name}} {{CreatedAt}} {{UpdatedAt}}

我的问题是,我们可以在一个模板中使用一个或多个结构体,还是只能限制在一个结构体中使用?

英文:

I have struct that has JSON field something like this:

detail := &Detail{
Name string
Detail json.RawMessage
}

template looks like this:

detail = At {{Name}} {{CreatedAt}} {{UpdatedAt}}

My question can we use one or more structs for a single template or it is restricted to only one struct.

答案1

得分: 6

你可以传递任意数量的内容。由于你没有提供太多的示例供我参考,所以我会做一些假设,但是下面是处理的方法:

  1. // 简写 - 很有用!
  2. type M map[string]interface{}
  3. func SomeHandler(w http.ResponseWriter, r *http.Request) {
  4. detail := Detail{}
  5. // 从数据库、API响应等获取
  6. populateDetail(&detail)
  7. user := User{}
  8. populateUser(&user)
  9. // 获取会话,设置头部等
  10. // 假设tmpl已经是一个已定义的*template.Template
  11. tmpl.RenderTemplate(w, "index.tmpl", M{
  12. // 我们可以传递任意数量的内容
  13. "detail": detail,
  14. "profile": user,
  15. "status": "", // 仅作为示例
  16. })
  17. }

... 还有我们的模板:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. // 使用 "with"
  5. {{ with .detail }}
  6. {{ .Name }}
  7. {{ .CreatedAt }}
  8. {{ .UpdatedAt }}
  9. {{ end }}
  10. // ... 或者完全限定的方式
  11. // User有字段 "Name"、"Email"、"Address"。我们只使用两个。
  12. 你好,{{ .profile.Name }}!
  13. 登录邮箱为 {{ .profile.Email }}
  14. </body>
  15. </html>

希望能够解答你的问题。

英文:

You can pass as many things as you like. You haven't provided much of an example to work with, so I'm going to assume a few things, but here's how you would tackle it:

  1. // Shorthand - useful!
  2. type M map[string]interface
  3. func SomeHandler(w http.ResponseWriter, r *http.Request) {
  4. detail := Detail{}
  5. // From a DB, or API response, etc.
  6. populateDetail(&amp;detail)
  7. user := User{}
  8. populateUser(&amp;user)
  9. // Get a session, set headers, etc.
  10. // Assuming tmpl is already a defined *template.Template
  11. tmpl.RenderTemplate(w, &quot;index.tmpl&quot;, M{
  12. // We can pass as many things as we like
  13. &quot;detail&quot;: detail,
  14. &quot;profile&quot;: user,
  15. &quot;status&quot;: &quot;&quot;, // Just an example
  16. }
  17. }

... and our template:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;body&gt;
  4. // Using &quot;with&quot;
  5. {{ with .detail }}
  6. {{ .Name }}
  7. {{ .CreatedAt }}
  8. {{ .UpdatedAt }}
  9. {{ end }}
  10. // ... or the fully-qualified way
  11. // User has fields &quot;Name&quot;, &quot;Email&quot;, &quot;Address&quot;. We&#39;ll use just two.
  12. Hi there, {{ .profile.Name }}!
  13. Logged in as {{ .profile.Email }}
  14. &lt;/body&gt;
  15. &lt;/html&gt;

Hope that clarifies.

huangapple
  • 本文由 发表于 2014年8月15日 23:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/25329647.html
匿名

发表评论

匿名网友

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

确定