【问题标题】:Html template use on golang在 golang 上使用 Html 模板
【发布时间】:2015-02-20 05:25:34
【问题描述】:

对不起,我是初学者,我阅读了 golang.docs,但没有很好地理解。 我已经:index.html:

<html>
<head>
</head>
<body>
<form action type="checkbox" name="test" value="A" {{.checked}}>
<input type="submit" value="save">
</body>
</html>

在 main.go 中 如果用户单击保存按钮然后选中复选框重定向该页面并显示复选框选中

【问题讨论】:

  • 问题是......?
  • 如何将值设置为 {{.checked}}
  • 您应该编辑您的帖子并写下您到目前为止所做的事情以及您遇到问题的地方。否则我们无法为您提供帮助。

标签: html http go go-html-template


【解决方案1】:

您可以在地图中发送变量。例如:

package main

import (
    "bytes"
    "fmt"
    "text/template"
)

func main() {
    t, _ := template.New("hi").Parse("Hi {{.name}}")
    var doc bytes.Buffer
    t.Execute(&doc, map[string]string{"name": "Peter"})
    fmt.Println(doc.String()) //Hi Peter
}

【讨论】:

    【解决方案2】:

    . 在 go 代码中定义。

    请提供执行模板的 go 代码的 sn-p,类似于以下代码:

        t, _ := template.ParseFiles(tmpl + ".html")
        t.Execute(w, data) // the data must feature the field "checked"
    

    或者

        templates.ExecuteTemplate(w, tmpl+".html", data) // the data must feature the field "checked"
    

    您可以将任何类型(接口{})传递给将模板作为“数据”执行的函数。通常是 Struct 或 Map[string]string。

    如何设置勾选 根据发布的表单,可能在 main.go at 处理程序中设置了“已检查”。

    阅读文档并更好地解释它。请

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多