英文:
Go text/template Form handling, with custom FuncMap - function returning blank string outputs garbage
问题
使用Go的html/template功能,结合自定义的FuncMap。我定义的自定义函数"isChecked(checkName string, form url.Values)"会在表单中存在名为checkName的复选框时返回字符串"checked",否则返回空字符串。它的功能正常,但有个奇怪的问题是,在未选中的复选框的完成HTML中,我看到了字符串"ZgotmplZ",而实际上应该没有任何字符串。以下是设置FuncMap的main.go程序:
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.New("").Funcs(fm).ParseFiles("templates/check-box.html"))
}
var fm = template.FuncMap{
"isChecked": isChecked,
}
func isChecked(checkName string, form url.Values) string {
if form.Has(checkName) {
return "checked"
}
return ""
}
func checkboxHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatalln(err)
}
err = tpl.ExecuteTemplate(w, "check-box.html", r.Form)
if err != nil {
log.Fatalln(err)
}
}
func main() {
http.HandleFunc("/checkbox/", checkboxHandler)
err := http.ListenAndServe(":8080", nil)
}
以下是模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox Control</title>
</head>
<body>
<h1>Go html/template Form Handling - Checkbox</h1>
<div>
Form Data: {{.}}
</div>
<form action="/checkbox/" method="POST">
<div>
<label for="checkbox">Check Box</label>
<input type="checkbox" name="check1" id="checkbox" value="check1" {{isChecked "check1" .}}>Check 1
<input type="checkbox" name="check2" id="checkbox" value="check2" {{isChecked "check2" .}}>Check 2
<input type="checkbox" name="check3" id="checkbox" value="check3" {{isChecked "check3" .}}>Check 3
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>
提交表单并选中复选框的结果是正确的。但是当我查看源代码时,对于未选中的复选框,我得到了一个奇怪的字符串"ZgotmplZ"(换句话说,似乎FuncMap中的自定义函数没有为未选中的复选框返回空字符串):
<div>
<label for="checkbox">Check Box</label>
<input type="checkbox" name="check1" id="checkbox" value="check1" checked>Check 1
<input type="checkbox" name="check2" id="checkbox" value="check2" ZgotmplZ>Check 2
<input type="checkbox" name="check3" id="checkbox" value="check3" ZgotmplZ>Check 3
</div>
同时,我想知道是否有其他建议可以将复选框的"checked"状态传递给HTML模板。
谢谢!
英文:
Using Go html/template functionality, with custom FuncMap. The custom function I've defined "isChecked(checkName string, form url.Values)" returns the string "checked" if the checkbox with checkName exists in the form, and a blank string if it does not. It works fine, but the curious thing is on the completed HTML for an un-checked checkbox, I am seeing the string "ZgotmplZ" where there should be no string at all. Here is the main.go program that sets up the FuncMap:
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.New("").Funcs(fm).ParseFiles("templates/check-box.html"))
}
var fm = template.FuncMap{
"isChecked": isChecked,
}
func isChecked(checkName string, form url.Values) string {
if form.Has(checkName) {
return "checked"
}
return ""
}
func checkboxHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatalln(err)
}
err = tpl.ExecuteTemplate(w, "check-box.html", r.Form)
if err != nil {
log.Fatalln(err)
}
}
func main() {
http.HandleFunc("/checkbox/", checkboxHandler)
err := http.ListenAndServe(":8080", nil)
}
Here is the template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox Control</title>
</head>
<body>
<h1>Go html/template Form Handling - Checkbox</h1>
<div>
Form Data: {{.}}
</div>
<form action="/checkbox/" method="POST">
<div>
<label for="checkbox">Check Box</label>
<input type="checkbox" name="check1" id="checkbox" value="check1" {{isChecked "check1" .}}>Check 1
<input type="checkbox" name="check2" id="checkbox" value="check2" {{isChecked "check2" .}}>Check 2
<input type="checkbox" name="check3" id="checkbox" value="check3" {{isChecked "check3" .}}>Check 3
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>
The result of submitting the form and checking the checkboxes is good. But when I examine source, I get this funny string "ZgotmplZ" for checkboxes that are not checked (in other words, it seems the custom function in the FuncMap is not returning a blank string for un-checked checkboxes):
<div>
<label for="checkbox">Check Box</label>
<input type="checkbox" name="check1" id="checkbox" value="check1" checked>Check 1
<input type="checkbox" name="check2" id="checkbox" value="check2" ZgotmplZ>Check 2
<input type="checkbox" name="check3" id="checkbox" value="check3" ZgotmplZ>Check 3
</div>
Also wondering if anyone has other suggestions on how to propate checkbox "checked" states to an html template.
Thanks!
答案1
得分: 0
模板发出ZgotmplZ,因为它不喜欢在属性上下文中出现空字符串。通过以下方式进行修复。
从isChecked函数返回bool类型:
func isChecked(checkName string, form url.Values) bool {
return form.Has(checkName)
}
在模板中使用if语句:
{{if isChecked "check1" .}}checked{{end}}
英文:
The template emits ZgotmplZ because it does not like an empty string in an attribute context. Fix by doing the following.
Return bool from isChecked:
func isChecked(checkName string, form url.Values) bool {
return form.Has(checkName)
}
Use an if action in the template:
{{if isChecked "check1" .}}checked{{end}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论