第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

模板不呈現(xiàn)任何內容,也沒有錯誤,但狀態(tài)為 200

模板不呈現(xiàn)任何內容,也沒有錯誤,但狀態(tài)為 200

Go
手掌心 2022-04-20 20:54:13
我在一個簡單的 HTTP 服務器上玩 Go:// var tpl = template.Must(template.New("").Funcs(template.FuncMap{"isRegistered": isRegistered}).ParseGlob("templates/*")) // functions will be added latervar tpl = template.Must(template.ParseGlob("templates/*"))func contact(w http.ResponseWriter, r *http.Request) {    //// defined templates are: "home.html", "layout", "layout.html", "contact.html", "body"    log.Println("in handler: ", tpl.DefinedTemplates())    err := tpl.ExecuteTemplate(w, "contact.html", nil)    if err != nil {        fmt.Println(err) // no error displayed    }    // fmt.Fprintf((w), "write") - This works fine}func main() {    log.Println("Serving on 8888 port")    http.HandleFunc("/contact", contact)    http.ListenAndServe(":8888", nil)}{{define "layout"}}<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>{{.Title}}</title>    <meta name="description" content="{{.Description}}">    <link rel="canonical" href="{{.Canonical}}" /></head><body>{{template "body" .}}</body></html>{{end}}{{define "body"}}<h1>Contact us page</h1><p>    Your name is...</p>{{end}}localhost:8888/contact返回OK 200 和空正文。我用了這個例子:https ://stackoverflow.com/a/36643663/2110953但是我將來還需要添加模板函數(shù): var tpl = template.Must(template.New("").Funcs(template.FuncMap{"isRegistered": isRegistered}).ParseGlob("templates/*"))
查看完整描述

2 回答

?
森欄

TA貢獻1810條經(jīng)驗 獲得超5個贊

你contact.html不會“渲染”任何東西。它只是定義body模板,但不包括它(執(zhí)行它)。


要執(zhí)行模板(在模板內),您可以使用{{template}}操作。要定義和執(zhí)行模板,您可以使用{{block}}操作。


模板操作:


{{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.


{{block "name" pipeline}} T1 {{end}}

    A block is shorthand for defining a template

        {{define "name"}} T1 {{end}}

    and then executing it in place

        {{template "name" pipeline}}

    The typical use is to define a set of root templates that are

    then customized by redefining the block templates within.

如果您的目標是在所有頁面中都有一個“固定”的頁眉和頁腳,那么您必須重新構建您的模板。在某處定義了一個header和模板,頁面應該將它們作為第一個和最后一個元素包含在內。footer請參閱如何使用結構或變量值的字段作為模板名稱?


查看完整回答
反對 回復 2022-04-20
?
尚方寶劍之說

TA貢獻1788條經(jīng)驗 獲得超4個贊

更新:所以我只需要創(chuàng)建一個頁眉和頁腳模板:


{{template "header" .}}


<h1>Contact us page</h1>


<p>

    Your name is...

</p>



{{template "footer" .}}

{{define "header"}}

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>{{.Title}}</title>

    <meta name="description" content="{{.Description}}">

    <link rel="canonical" href="{{.Canonical}}" />

</head>

<body>

{{end}}

{{define "footer"}}

</body>

</html>

{{end}}

它工作得很好


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號