gpt4 book ai didi

html - go - 调用 "html/template"时没有足够的参数。必须

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:17 27 4
gpt4 key购买 nike

我在 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 函数运行服务器时,控制台抛出一个错误:

调用“html/template”时没有足够的参数。必须

如果我这样写:

templates,err:=template.Must(t)

它也会抛出同样的错误,另外:

分配计数不匹配: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}}

index.html模板:

<!DOCTYPE html>
<html>
{{define "title"}}Homepage{{end}}
<body>
{{define "index"}}
<div class="wrapper">
<div class="page-content">
<div class="container">
<div class="left">
<img src="../public/images/img_landing_page_mac.png">
</div>
<div class="right">
<h2 style="font-size: 33px; letter-spacing: 5px">Organize <br>Modern Knowledge<br> for Mankind</h2>
<p style="font-size: 20px;margin-top: 35px;letter-spacing: 4px">Consume, Colect and Revisit <br>Knowledge at Your Fingertips</p>
<a href="#" style="margin-top: 80px;display: inline-block;margin-left: -17px"><img src="../public/images/btn_get_chrome.png"></a>
</div>
</div>
</div>
</div>
{{end}}
</body>
</html>

我错过了什么吗?我检查了 "html/template".Must 的原型(prototype),但没有明白发生了什么

最佳答案

你不需要调用 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)
}
}

我相信上面的函数应该做你想做的......

关于html - go - 调用 "html/template"时没有足够的参数。必须,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com