为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 239 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号