讀docs不是特別有幫助,我想知道,如果結構在html/templates中,是否有任何方法可以在所有頁面中使用常量頁眉/頁腳?
{{header}}
{{content that always changes}}
{{footer}}
是golang實現的。
讀docs不是特別有幫助,我想知道,如果結構在html/templates中,是否有任何方法可以在所有頁面中使用常量頁眉/頁腳?
{{header}}
{{content that always changes}}
{{footer}}
是golang實現的。
代碼,以使它對標準輸出
t := template.Must(template.ParseFiles("main.tmpl", "head.tmpl", "foot.tmpl"))
t.Execute(os.Stdout, nil)
main.tmpl:
{{template "header" .}}
<p>main content</p>
{{template "footer" .}}
foot.tmpl:
{{define "footer"}}
<footer>This is the foot</footer>
{{end}}
head.tmpl:
{{define "header"}}
<header>This is the head</header>
{{end}}
這將導致:
使用html/template將是極其相似。
如果您錯過了,請閱讀['text/template'](https://golang.org/pkg/text/template/)文檔。 –
@TimCooper我錯過了:O – m0meni
那麼使用命名模板會如何? '{{{template「name」}} \t具有指定名稱的模板將以零數據執行。 – m0meni