英文:
In html/templates is there any way to have a constant header/footer across all pages?
问题
阅读文档并没有特别有帮助,我想知道是否可以用golang实现以下结构:
{{header}}
{{content that always changes}}
{{footer}}
英文:
Reading the docs wasn't particularly helpful and I want to know if the structure
{{header}}
{{content that always changes}}
{{footer}}
is achievable with golang.
答案1
得分: 2
- 将其渲染到标准输出的代码
t := template.Must(template.ParseFiles("main.tmpl", "head.tmpl", "foot.tmpl"))
t.Execute(os.Stdout, nil)
- main.tmpl:
{{template "header" .}}
<p>主要内容</p>
{{template "footer" .}}
- foot.tmpl:
{{define "footer"}}
<footer>这是页脚</footer>
{{end}}
- head.tmpl:
{{define "header"}}
<header>这是页头</header>
{{end}}
结果将会是:
<header>这是页头</header>
<p>主要内容</p>
<footer>这是页脚</footer>
使用html/template的方式非常类似。
英文:
Using text/template:
-
Code to render it to Stdout
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}}
This will result in:
<header>This is the head</header>
<p>main content</p>
<footer>This is the foot</footer>
Using html/template will be extremely similar.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论