Go text/template Form handling, with custom FuncMap – function returning blank string outputs garbage

huangapple go评论111阅读模式
英文:

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程序:

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "log"
  6. "net/http"
  7. "net/url"
  8. )
  9. var tpl *template.Template
  10. func init() {
  11. tpl = template.Must(template.New("").Funcs(fm).ParseFiles("templates/check-box.html"))
  12. }
  13. var fm = template.FuncMap{
  14. "isChecked": isChecked,
  15. }
  16. func isChecked(checkName string, form url.Values) string {
  17. if form.Has(checkName) {
  18. return "checked"
  19. }
  20. return ""
  21. }
  22. func checkboxHandler(w http.ResponseWriter, r *http.Request) {
  23. err := r.ParseForm()
  24. if err != nil {
  25. log.Fatalln(err)
  26. }
  27. err = tpl.ExecuteTemplate(w, "check-box.html", r.Form)
  28. if err != nil {
  29. log.Fatalln(err)
  30. }
  31. }
  32. func main() {
  33. http.HandleFunc("/checkbox/", checkboxHandler)
  34. err := http.ListenAndServe(":8080", nil)
  35. }

以下是模板:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Checkbox Control</title>
  8. </head>
  9. <body>
  10. <h1>Go html/template Form Handling - Checkbox</h1>
  11. <div>
  12. Form Data: {{.}}
  13. </div>
  14. <form action="/checkbox/" method="POST">
  15. <div>
  16. <label for="checkbox">Check Box</label>
  17. <input type="checkbox" name="check1" id="checkbox" value="check1" {{isChecked "check1" .}}>Check 1
  18. <input type="checkbox" name="check2" id="checkbox" value="check2" {{isChecked "check2" .}}>Check 2
  19. <input type="checkbox" name="check3" id="checkbox" value="check3" {{isChecked "check3" .}}>Check 3
  20. </div>
  21. <div>
  22. <button type="submit">Submit</button>
  23. </div>
  24. </form>
  25. </body>
  26. </html>

提交表单并选中复选框的结果是正确的。但是当我查看源代码时,对于未选中的复选框,我得到了一个奇怪的字符串"ZgotmplZ"(换句话说,似乎FuncMap中的自定义函数没有为未选中的复选框返回空字符串):

  1. <div>
  2. <label for="checkbox">Check Box</label>
  3. <input type="checkbox" name="check1" id="checkbox" value="check1" checked>Check 1
  4. <input type="checkbox" name="check2" id="checkbox" value="check2" ZgotmplZ>Check 2
  5. <input type="checkbox" name="check3" id="checkbox" value="check3" ZgotmplZ>Check 3
  6. </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:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;html/template&quot;
  5. &quot;log&quot;
  6. &quot;net/http&quot;
  7. &quot;net/url&quot;
  8. )
  9. var tpl *template.Template
  10. func init() {
  11. tpl = template.Must(template.New(&quot;&quot;).Funcs(fm).ParseFiles(&quot;templates/check-box.html&quot;))
  12. }
  13. var fm = template.FuncMap{
  14. &quot;isChecked&quot;: isChecked,
  15. }
  16. func isChecked(checkName string, form url.Values) string {
  17. if form.Has(checkName) {
  18. return &quot;checked&quot;
  19. }
  20. return &quot;&quot;
  21. }
  22. func checkboxHandler(w http.ResponseWriter, r *http.Request) {
  23. err := r.ParseForm()
  24. if err != nil {
  25. log.Fatalln(err)
  26. }
  27. err = tpl.ExecuteTemplate(w, &quot;check-box.html&quot;, r.Form)
  28. if err != nil {
  29. log.Fatalln(err)
  30. }
  31. }
  32. func main() {
  33. http.HandleFunc(&quot;/checkbox/&quot;, checkboxHandler)
  34. err := http.ListenAndServe(&quot;:8080&quot;, nil)
  35. }

Here is the template:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;title&gt;Checkbox Control&lt;/title&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;h1&gt;Go html/template Form Handling - Checkbox&lt;/h1&gt;
  11. &lt;div&gt;
  12. Form Data: {{.}}
  13. &lt;/div&gt;
  14. &lt;form action=&quot;/checkbox/&quot; method=&quot;POST&quot;&gt;
  15. &lt;div&gt;
  16. &lt;label for=&quot;checkbox&quot;&gt;Check Box&lt;/label&gt;
  17. &lt;input type=&quot;checkbox&quot; name=&quot;check1&quot; id=&quot;checkbox&quot; value=&quot;check1&quot; {{isChecked &quot;check1&quot; .}}&gt;Check 1
  18. &lt;input type=&quot;checkbox&quot; name=&quot;check2&quot; id=&quot;checkbox&quot; value=&quot;check2&quot; {{isChecked &quot;check2&quot; .}}&gt;Check 2
  19. &lt;input type=&quot;checkbox&quot; name=&quot;check3&quot; id=&quot;checkbox&quot; value=&quot;check3&quot; {{isChecked &quot;check3&quot; .}}&gt;Check 3
  20. &lt;/div&gt;
  21. &lt;div&gt;
  22. &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
  23. &lt;/div&gt;
  24. &lt;/form&gt;
  25. &lt;/body&gt;
  26. &lt;/html&gt;

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):

  1. &lt;div&gt;
  2. &lt;label for=&quot;checkbox&quot;&gt;Check Box&lt;/label&gt;
  3. &lt;input type=&quot;checkbox&quot; name=&quot;check1&quot; id=&quot;checkbox&quot; value=&quot;check1&quot; checked&gt;Check 1
  4. &lt;input type=&quot;checkbox&quot; name=&quot;check2&quot; id=&quot;checkbox&quot; value=&quot;check2&quot; ZgotmplZ&gt;Check 2
  5. &lt;input type=&quot;checkbox&quot; name=&quot;check3&quot; id=&quot;checkbox&quot; value=&quot;check3&quot; ZgotmplZ&gt;Check 3
  6. &lt;/div&gt;

Also wondering if anyone has other suggestions on how to propate checkbox "checked" states to an html template.

Thanks!

答案1

得分: 0

模板发出ZgotmplZ,因为它不喜欢在属性上下文中出现空字符串。通过以下方式进行修复。

从isChecked函数返回bool类型:

  1. func isChecked(checkName string, form url.Values) bool {
  2. return form.Has(checkName)
  3. }

在模板中使用if语句:

  1. {{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:

  1. func isChecked(checkName string, form url.Values) bool {
  2. return form.Has(checkName)
  3. }

Use an if action in the template:

  1. {{if isChecked &quot;check1&quot; .}}checked{{end}}

huangapple
  • 本文由 发表于 2022年10月23日 04:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/74166762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定