2016-10-03 1 views
0

페이지, 머리글 및 본문이 있다고 가정 해 보겠습니다. 머리글에는 링크가 있고 클릭하면 본문이 변경되지만 머리글은 그대로 유지됩니다. html/template 라이브러리를 사용하여 이것을 구축하는 것은 쉽지만, 방금 전 데이터베이스에서 헤더의 정보를 가져 오는 새로운 페이지를 다시 보내면 바보처럼 보일 수도 있습니다. 나는 url에 따라 신체 템플릿을 어떻게 바꿀 것인가. 그것은 일부 JS 시간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 ... 
} 

`

가 : 여기

내가 가진 무엇인가?

답변

0

이렇게하면 html 폴더에 html 파일을 넣고 html과 js 파일을 추가하십시오.

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

}

http://localhost:9000/

아래를 찾아 볼