登录
首页 >  Golang >  Go问答

golang html/template定义不起作用

来源:stackoverflow

时间:2024-02-17 09:42:29 179浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《golang html/template定义不起作用》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

html/template 模块具有 define/template 功能,我试图使用该功能来更轻松地仅更改包含我网站的全局基础的一个文件,但它无法按预期工作。

base.html

{{define "base"}}



{{template "content"}}


{{end}}

index.html:

{{template "base"}}

{{define "content"}}
i'm page 1
{{end}}

blog.html:

{{template "base"}}

{{define "content"}}
I'm page 2
{{end}}

当我调用index.html时,我期望得到im页面1,当我调用blog.html时,我期望得到im页面2,但是对于这两个文件,我得到im页面1作为输出。 (我不知道模板末尾的点有什么作用,但所有这些组合都不起作用)。


正确答案


您不能像这样重新定义和替换模板。反之亦然:将index.html和blog.html分别定义为page1和page2,将常用的header.html和footer.html导入其中。

以上就是《golang html/template定义不起作用》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>