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

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

不替換占位符

不替換占位符

Go
絕地無雙 2022-08-01 14:59:59
我正在試驗這個包。我也已經管理了例如頁眉,頁腳,導航欄等包含在基本模板中:http/template{{ define "base" }}    <!DOCTYPE html>    <html>    <!-- Start Head -->    <head>        {{ template "head" }}    </head>    <!-- End Head -->    <!-- Start Body -->    <body>    {{ template "navbar" }}    {{ template "content" }}    {{ template "footer" }}    </body>    <!-- End Body -->    </html>{{ end }}404 頁:{{ define "content" }}    [...]                    <h1 class="text-light text-right">404</h1>                    <small>{{.CurrentURL}}</small>    [...]{{ end }}因此,此處的變量應替換為當前 URL。但是,這僅在網站上顯示為空 ():CurrentURL""<small></small>但現在我想替換一個變量,它在網頁上僅顯示為“”。Go 代碼: 解析器:func (parser *TemplateParser) ParseTemplate(name string) (tpl *template.Template, err error) {  root, err := template.New("root").Parse(rootTmpl)  // ...  return root.ParseFiles(files...)}路線:func (ws *WebServer) Exec(name string, r *http.Request, w http.ResponseWriter, data map[string]interface{}) (err error) {    // ...    // add default data    data["CurrentURL"] = r.URL.RequestURI()    // ...    return tpl.Execute(w, data)}即使使用數組,我也不能使用等:range    type Test struct {        CurrentURL string        C []string    }    t := Test{        CurrentURL: "Current URL",        C: []string {"C1", "c2", "ccc4"},    }    tpl.Execute(w, t) <ul>{{range .C}}  <li>{{.}}</li>{{end}}</ul><!-- No <li></li> is created -->我做錯了什么?
查看完整描述

2 回答

?
www說

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

您必須將上下文傳遞給實例化模板。用

{{ template "content" .}}

將數據傳遞到模板。.content


查看完整回答
反對 回復 2022-08-01
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

您不會將任何數據傳遞給子模板。根據文檔:


{{template "name"}}

    The template with the specified name is executed with nil data.


{{template "name" pipeline}}

    The template with the specified name is executed with dot set

    to the value of the pipeline.


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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