杜松子酒模板覆盖部分模板

我正在使用杜松子酒和它的功能。一个,如果它们是html模板渲染。因此,本着DRY的精神,我想创建一个包含所有常见html标签等的模板,并为不同的页面主体提供一个插槽。base.html


从本质上讲,这是base.html


{{define "base"}}

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>


{{ template "main" . }}


</body>

</html>

{{end}}

然后我创建了一个名为”home.html


{{template "base" .}}


{{define "main"}}

    <div class="container mt-5">

        Hello

    </div>

{{end}}

我在这个页面上遵循了这个精彩的指南,它就像一个魅力。


问题

但是当我尝试添加另一个具有不同正文的页面时,例如:subpage.html


{{template "base" .}}


{{define "main"}}

<div class="container">

    <div>

        <h2>This page is still in progress</h2>

    </div>

</div>

{{end}}

然后,由 gins 或 选择的最后一个模板将显示在每个页面上。在这种情况下,这是内容。我该如何解决这个问题。默认情况下是否有可能实现此行为?LoadHTMLFilesLoadHTMLGlobsubpage.html


慕工程0101907
浏览 113回答 1
1回答

慕仙森

你可以做这样的事情:base.html{{ define "top" }}<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1"></head><body>{{ end }}{{ define "bottom" }}</body></html>{{ end }}home.html{{ template "top" . }}<div class="container mt-5">&nbsp; &nbsp; Hello</div>{{ template "bottom" . }}subpage.html{{ template "top" . }}<div class="container">&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <h2>This page is still in progress</h2>&nbsp; &nbsp; </div></div>{{ template "bottom" . }}然后,确保您使用的是文件的基本名称:// in the home handler use the followingc.HTML(http.StatusOK, "home.html", data)// in the subpage handler use the followingc.HTML(http.StatusOK, "subpage.html", data)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go