为了账号安全,请及时绑定邮箱和手机立即绑定

go - 使用继承呈现 html/模板

go - 使用继承呈现 html/模板

Go
喵喵时光机 2021-12-20 16:32:53
我有两个 html 模板,index.html扩展base.htmlbase.html 是这样的:{{ define "base" }}<html><head>    <meta charget="utf-8">    <title>{{ template "title" . }}</title>    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>    <script type="text/javascript" src="/js/isotope.pkgd.min.js"></script>    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">    <link rel="stylesheet" type="text/css" href="/css/style.css"></head><body>    {{ template "index" . }}</body></html>{{ end }}并且index.html:{{ define "title" }}Homepage{{ end }}{{ define "index" }}<div class="wrapper">    <div class="page-content">        <div class="container">            <div class="left">                <img src="../public/images/img_landing_page_mac.png">            </div>            <div class="right">                <h2 style="font-size: 33px; letter-spacing: 5px">Organize <br>Modern Knowledge<br> for Mankind</h2>                <p style="font-size: 20px;margin-top: 35px;letter-spacing: 4px">Consume, Colect and Revisit <br>Knowledge at Your Fingertips</p>                <a href="#" style="margin-top: 80px;display: inline-block;margin-left: -17px"><img src="../public/images/btn_get_chrome.png"></a>            </div>        </div>    </div></div>{{ end }}它应该在使用回调处理程序在浏览器上请求路径时呈现:func IndexHandler(w http.ResponseWriter,r *http.Request){    files:=[]string{"base","index"}    util.RenderTemplate(w,nil,files...)}启动服务器后,我在浏览器上请求该路径,但根本没有渲染任何内容。我错过了什么?这里好像没有领悟到继承我遵循本教程,尝试使用继承/扩展来呈现模板:https://elithrar.github.io/article/approximating-html-template-inheritance/
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

define动作不执行模板,只templateblock行动做。很可能您只想define从基本模板(第一行和最后一行)中删除,它会按预期工作。

或者您可以使用Template.ExecuteTemplatefunction 而不是Template.Execute. 它接受模板的名称: err = templates.ExecuteTemplate(w, "base", data)

或者,如果您使用的是 Go1.6 或更高版本,则可以尝试使用blockaction 代替define.

另外,请考虑使用gofmt


查看完整回答
反对 回复 2021-12-20
  • 1 回答
  • 0 关注
  • 276 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号