【问题标题】:Minimal diffing with golang templates?与 golang 模板的最小差异?
【发布时间】:2016-10-03 04:40:22
【问题描述】:

假设我有一个页面,它是一个页眉和一个正文。标题中是链接,单击时正文会更改,但标题仍然存在。 使用html/template 库构建它很容易,但如果我只是发回一个全新的页面(每次都从数据库中获取标题中的信息),这似乎也很愚蠢。我猜如何根据url 切换正文模板。

这是我所拥有的:

`

{{template "GlobalNav"}} 
 {{template "GroupHeader" .Header }} 
 {{ if eq .Active "" }}
 {{ template "GroupBody" .Body }}
 {{ else if eq .Active "papers" }}
 {{ template "GroupPapers" .Body }}
 {{ else if eq .Active "projects" }}
 {{ template "GroupProjects" .Body }}
 {{ end }}`

Server Side:

`http.HandleFunc("/g/", Groups)
http.HandleFunc("/g/papers", GroupsPapers)
http.HandleFunc("/g/projects", GroupsProjects)
func Groups() {
 header := fromDBHeader(id)
body := fromDBMain(id)
render Home template ...
}
func GroupsPapers() {
  header := fromDBHeader(id)
   body := fromDBPapers(id)
   render Paper template ...
   }
func GroupsProjects() {
header := fromDBHeader(id)
body := fromDBProjects(id)
render Project template ...
}

`

是时候来点 JS 了吗?

【问题讨论】:

    标签: go go-templates


    【解决方案1】:

    试试这种方式把html文件放到html文件夹里,添加html和js文件。

    func webServer() {
    http.Handle(
        "/",
        http.StripPrefix(
            "/",
            http.FileServer(http.Dir("html")),
        ),
    )
    http.ListenAndServe(":9000", nil)
    

    }

    并在http://localhost:9000/ 下浏览它

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 2013-10-28
      • 2013-03-14
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多