英文:
How to send variable and its value to html
问题
我已经用Go语言写了一小段代码。
func loginHandler(w http.ResponseWriter, r *http.Request) {
log.Println("loginHandler")
log.Println("request url is", r.RequestURI)
log.Println("request method", r.Method)
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("request body is", string(requestbody))
if r.Method == "POST" {
us, err := globalSessions.SessionStart(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
us.Set("LoggedInUserID", "000000")
w.Header().Set("Location", "/auth")
w.WriteHeader(http.StatusFound)
return
}
outputHTML(w, r, "static/login.html")
}
func outputHTML(w http.ResponseWriter, req *http.Request, filename string) {
log.Println("outputHTML")
requestbody, _ := ioutil.ReadAll(req.Body)
log.Println("request body is", string(requestbody))
log.Println("request body is", requestbody)
file, err := os.Open(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer file.Close()
fi, _ := file.Stat()
http.ServeContent(w, req, file.Name(), fi.ModTime(), file)
}
在这段代码中,我将重定向到login.html
。现在我想要发送一个变量,假设它是一个名为testvariable
的字符串,并将其值传递给login.html
。
英文:
I have written a small piece of code in go
func loginHandler(w http.ResponseWriter, r *http.Request) {
log.Println("loginHandler")
log.Println("request url is", r.RequestURI)
log.Println("request method", r.Method)
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("request body is", string(requestbody))
if r.Method == "POST" {
us, err := globalSessions.SessionStart(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
us.Set("LoggedInUserID", "000000")
w.Header().Set("Location", "/auth")
w.WriteHeader(http.StatusFound)
return
}
outputHTML(w, r, "static/login.html")
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
func outputHTML(w http.ResponseWriter, req *http.Request, filename string) {
log.Println("outputHTML")
requestbody, _ := ioutil.ReadAll(req.Body)
log.Println("request body is", string(requestbody))
log.Println("request body is", requestbody)
file, err := os.Open(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer file.Close()
fi, _ := file.Stat()
http.ServeContent(w, req, file.Name(), fi.ModTime(), file)
}
<!-- end snippet -->
in this code i am redirecting to login.html . now i want to send a variable let it be some string called testvariable and its value to login.html.
答案1
得分: 3
要在你的HTML中显示值,你可以使用Go的html/template
包。
首先,你需要指定在HTML页面中希望出现值的位置,使用html/template
包,你可以通过模板操作来实现。
"操作" - 数据评估或控制结构 - 由"{{"和"}}"界定
接下来,你需要放弃http.ServeContent
函数,因为它不知道如何渲染模板,而是可以使用Execute来显示登录页面以及你的值。
这是一个示例:
login.html:
<html>
<body>
<h1>{{.MyVar}}</h1>
</body>
</html>
outputHTML:
func outputHTML(w http.ResponseWriter, filename string, data interface{}) {
t, err := template.ParseFiles(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := t.Execute(w, data); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
以及你的loginHandler:
func loginHandler(w http.ResponseWriter, r *http.Request) {
// 做你需要做的事情
myvar := map[string]interface{}{"MyVar": "Foo Bar Baz"}
outputHTML(w, "static/login.html", myvar)
}
在这里阅读更多关于模板的信息:html/template,有关如何编写模板本身的信息,请参阅text/template的文档。
英文:
To be able to display values in your html you can use Go's html/template
package.
First you'll need to specify where in the html page you want your values to appear, using the html/template
package you can do that with template actions.
> "Actions"--data evaluations or control structures--are delimited by
> "{{" and "}}"
Next you'll need to drop the http.ServeContent
function as that does not know how to render templates, instead you can use Execute to display the login page together with your values.
Here's an example:
login.html:
<html>
<body>
<h1>{{.MyVar}}</h1>
</body>
</html>
outputHTML:
func outputHTML(w http.ResponseWriter, filename string, data interface{}) {
t, err := template.ParseFiles(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := t.Execute(w, data); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
And your loginHandler:
func loginHandler(w http.ResponseWriter, r *http.Request) {
// do whatever you need to do
myvar := map[string]interface{}{"MyVar": "Foo Bar Baz"}
outputHTML(w, "static/login.html", myvar)
}
Read more on templates here: html/template and for information about how to program the templates themselves, see the documentation for text/template
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论