英文:
go - Not enough arguments in call to "html/template".Must
问题
我在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}}
以及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
的原型,但不明白发生了什么。
英文:
I write a wrapper function in Golang for rendering template from multiple files like this:
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)
}
}
When I run server from main
function, the console throws an error:
not enough arguments in call to "html/template".Must
If I write like this:
templates,err:=template.Must(t)
It also throws the same error, plus:
assignment count mismatch: 2 = 1
I intend to use this function for a route handler in server:
func IndexHandler(w http.ResponseWriter, r *http.Request) {
files:=[]string{"base","index"}
util.RenderTemplate(w, nil, files...)
}
index.html
extends from base.html
using template nesting
base.html
template:
{{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}}
And index.html
template:
<!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>
Did I miss something? I checked the prototype of "html/template".Must
and didn't get what happened
答案1
得分: 1
你不需要调用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)
}
}
我相信上面的函数应该可以满足你的需求...
英文:
You do not need to call ParseFiles and Must, you can call one or the other
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)
}
}
I believe the func above should do what you want...
答案2
得分: 1
template.Must()
的签名如下:
func Must(t *Template, err error) *Template
Must()
的参数“巧合地”与ParseFiles()
和ParseGlob()
的返回值相同,因此您可以在Must()
内部使用这些函数,并且如果错误非空,则会引发panic。因此,您可以这样写:
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
英文:
template.Must()
has this signature:
func Must(t *Template, err error) *Template
the arguments to Must()
are "by coincidence" the same as the return values to ParseFiles()
and ParseGlob()
so you can use those functions inside Must()
and have the effect that it panics, if the error is non-nil. So you can say
t := template.Must(template.ParseFiles(....))
and don't care about the error checking. This is merely a convenience function, similar to all other Must()
functions throughout the standard library, such as regexp.MustCompile()
.
The implementation of Must()
is straightforward:
func Must(t *Template, err error) *Template {
if err != nil {
panic(err)
}
return t
}
See https://golang.org/src/text/template/helper.go?s=576:619#L11
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论