英文:
Go template extend and super?
问题
在Flask中,我们可以在模板中从base.html
进行扩展。在Go的标准模板库中,如何进行扩展或使用super()
?否则,如果我需要使用顶部栏,我将不得不手动复制和粘贴顶部栏的代码。请告诉我。
英文:
In Flask, we can extend from base.html
in template. How do I extend or super()
using Go's standard template library? Otherwise, in case that I need to use top bar, I would have to manually copy and paste code for top bar. Please let me know.
答案1
得分: 1
大致意思是,你会有一个父模板(我们称之为layout),它作为初始模板执行,而在这个父模板中会有类似于{{template "someName" .}}的内容。
参考链接:https://github.com/jadekler/git-go-websiteskeleton/blob/master/templates/layout.html#L40。这个存储库是一个非常轻量级的骨架,包含基本的Go包 - 你可能会在查看时找到一些价值。
英文:
The jist of it is that you would have some parent template (we'll call it layout) that is executed as the initial template, and inside that layout parent template is something like {{template "someName" .}}.
See: https://github.com/jadekler/git-go-websiteskeleton/blob/master/templates/layout.html#L40. That repo is a very lightweight skeleton with basic go packages - you may find some value in checking it out.
答案2
得分: 1
我遇到了关于模板的问题。我之前使用过各种支持继承的模板引擎。
为了解决这个限制,我实际上复制了标准的text/template包,删除了template.go中的重新定义错误和multi_test.go中的测试。这样可以在模板中重新定义模板/定义块。
我创建了一个GitHub仓库https://github.com/d2g/goti,其中包含了示例等内容。在这个仓库上我还有很多事情要做(例如标记版本等)[欢迎提交Pull请求]。
英文:
I ran into this issue with templates. I've used a variety of templating engines before that support inheritance.
To get round the limitation I've actually copied the standard text/template package to removing the redefinition error (from template.go) and test (from multi_test.go). This allows you to redefine templates/define blocks in templates.
I created a github repo https://github.com/d2g/goti which contains examples etc. There are loads of things I still need to do on the repo (Tag Versions etc) [Hint Pull requests welcome].
答案3
得分: 0
在Go语言中,你可以使用html/template包。
然后你可以定义头部、主体和底部,就像这样:
// header.tpl:
{{define "header"}}
<html>
<head></head>
{{end}}
// body.tpl:
{{template "header" .}}
{{define "body"}}
<body>
</body>
{{end}}
{{template "footer" .}}
// footer.tpl:
{{define "footer"}}
</html>
{{end}}
s1, _ := template.ParseFiles("header.tpl", "body.tpl", "footer.tpl") // 从多个文件中创建一个模板集合
s1.Execute(os.Stdout, nil)
参考:http://golangtutorials.blogspot.com/2011/11/go-templates-part-3-template-sets.html
英文:
In Go, You can using html/template
Then you can define header, body, and footer look like
// header.tpl:
{{define "header"}}
<html>
<head></head>
{{end}}
// body.tpl:
{{template "header" .}}
{{define "body"}}
<body>
</body>
{{end}}
{{template "footer" .}}
// footer.tpl:
{{define "footer"}}
</html>
{{end}}
s1, _ := template.ParseFiles("header.tpl", "body.tpl", "footer.tpl") //create a set of templates from many files.
s1.Execute(os.Stdout, nil)
Reference: http://golangtutorials.blogspot.com/2011/11/go-templates-part-3-template-sets.html
答案4
得分: 0
如果你正在寻找一个类似于jinja2/Django的模板语言,用于Go语言(支持模板继承和模板包含,就像之前提到的那样),你可以尝试使用pongo2。
英文:
If you're searching for a jinja2/Django-like template language for Go (which supports both template inheritance and template inclusion like mentioned before), you should give pongo2 a try.
答案5
得分: 0
我找到的一个解决方案是将所有内容引用到我的base.gohtml文件中,然后使用逻辑来确定应该包含哪个模板。
func dashboard(w http.ResponseWriter, req *http.Request) {
data := struct {
Page string
}{
"dashboard",
}
err := tpl.ExecuteTemplate(w, "base", data)
if err != nil {
log.Fatalln(err)
}
}
然后在我的base模板中:
headers, navs, css
{{if eq .Page "dashboard"}}
{{template "dashboard"}}
{{else if eq .Page "login"}}
{{template "login"}}
{{else}}
...
{{end}}
footers, scripts
英文:
One solution I found was to refer everything to my base.gohtml and then use logic to determine what template should be included.
func dashboard(w http.ResponseWriter, req *http.Request) {
data := struct {
Page string
}{
"dashboard",
}
err := tpl.ExecuteTemplate(w, "base", data)
if err != nil {
log.Fatalln(err)
}
}
and then inside my base template:
headers, navs, css
{{if eq .Page "dashboard"}}
{{template "dashboard"}}
{{else if .Page "login"}}
{{template "login"}}
{{else}}
...
{{end}}
footers, scripts
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论