2016-10-03 92 views
0

比方說,我有一個頁面,它是一個標題和一個正文。在標題中有鏈接,並點擊正文更改,但標題保持不變。 使用html/template庫來構建這個庫很容易,但如果我只是發回一整個新頁面(每次從數據庫獲取頭信息),看起來都很笨拙。我想如何根據url轉換出正文模板。與golang模板最小的區別?

以下是我有:

`

{{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?

回答

0

試試這個方法把html文件添加到html文件夾中添加html和js文件。

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

}

,瀏覽IT UNDER http://localhost:9000/