亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Go
慕工程0101907 2022-08-15 19:43:44
我正在使用杜松子酒和它的功能。一個,如果它們是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
查看完整描述

1 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

你可以做這樣的事情:


base.html


{{ define "top" }}

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <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">

    Hello

</div>


{{ template "bottom" . }}

subpage.html


{{ template "top" . }}


<div class="container">

    <div>

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

    </div>

</div>


{{ template "bottom" . }}

然后,確保您使用的是文件的基本名稱:


// in the home handler use the following

c.HTML(http.StatusOK, "home.html", data)


// in the subpage handler use the following

c.HTML(http.StatusOK, "subpage.html", data)


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號