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

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

go - 調用“html/template”時沒有足夠的參數。必須

go - 調用“html/template”時沒有足夠的參數。必須

Go
catspeake 2021-12-20 16:30:58
我在 Golang 中編寫了一個包裝函數,用于從多個文件渲染模板,如下所示:func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {    cwd, _ := os.Getwd()    for _,file:=range tmpl{        file=filepath.Join(cwd,"./view/"+file+".html")    }    t, err := template.ParseFiles(tmpl...)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    templates:=template.Must(t)    err = templates.Execute(w, data)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)    }}當我從main函數運行服務器時,控制臺拋出一個錯誤:not enough arguments in call to "html/template".Must如果我這樣寫:templates,err:=template.Must(t)它還拋出相同的錯誤,另外:assignment count mismatch: 2 = 1我打算將此函數用于服務器中的路由處理程序:func IndexHandler(w http.ResponseWriter, r *http.Request) {    files:=[]string{"base","index"}    util.RenderTemplate(w, nil, files...)}index.html從base.html使用模板嵌套擴展base.html 模板:{{define "base"}}<!DOCTYPE html><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}}我錯過了什么?我檢查了原型"html/template".Must并沒有得到發生了什么
查看完整描述

2 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

你不需要調用 ParseFiles 和 Must,你可以調用一個或另一個


func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {

    cwd, _ := os.Getwd()

    for _,file:=range tmpl{

        file=filepath.Join(cwd,"./view/"+file+".html")

    }

    t, err := template.ParseFiles(tmpl...)

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    err = t.Execute(w, data)

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

    }

}

我相信上面的 func 應該做你想做的......


查看完整回答
反對 回復 2021-12-20
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

template.Must() 有這個簽名:


func Must(t *Template, err error) *Template 

的參數Must()“巧合”與返回值相同ParseFiles(),ParseGlob()因此Must()如果錯誤非零,您可以在內部使用這些函數并產生恐慌的效果。所以你可以說


t := template.Must(template.ParseFiles(....))

并且不關心錯誤檢查。這只是一個方便的函數,類似于Must()整個標準庫中的所有其他函數,例如regexp.MustCompile().


的實現Must()很簡單:


func Must(t *Template, err error) *Template {

    if err != nil {

            panic(err)

    }

    return t

}

見https://golang.org/src/text/template/helper.go?s=576:619#L11


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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