英文:
content in html template is being replaced by template's file-location and not desired text
问题
我正在使用html/template包来在表单提交时提供一个模板。渲染的页面是模板文件的位置,而不是应该替换{{ .Title }}的文本。
所以在response.html中,{{ .Title }}显示为"Projects/Go/src/web/site/index",而不是"I feel that is"。
如何使{{ .Title }}被文本替换而不是文件位置?
以下是我的代码:
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"html/template"
"io/ioutil"
)
type Page struct {
Title string
Body []byte
}
func loadPage(title string) (*Page, error) {
filename := title + ".html"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, err := template.ParseFiles(tmpl + ".html")
if err != nil {
panic(err)
}
err = t.Execute(w, p)
fmt.Println(err)
}
func response(c web.C, w http.ResponseWriter, r *http.Request) {
p, err := loadPage("Projects/Go/src/web/site/index")
if err != nil {
p = &Page{Title: "I feel that is"}
panic(err)
}
renderTemplate(w, "Projects/Go/src/web/site/response", p)
}
func serveSingle(filename string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
}
}
func main() {
goji.Get("/", serveSingle("Projects/Go/src/web/site/index.html"))
goji.Handle("/ask", response)
goji.Serve()
}
希望对你有所帮助!
英文:
I'm using the html/template package to serve a template on submission of the form. The page that is a copy of that template is being rendered with the location of the template-file instead of the text that should replace {{ .Title }}
So in response.html, the {{ .Title }} is showing up as "Projects/Go/src/web/site/index" instead of "I feel that is "
How can I get the {{ .Title }} to be replaced by the text and not the file-location?
Here is my code:
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"html/template"
"io/ioutil"
)
type Page struct {
Title string
Body []byte
}
func loadPage(title string) (*Page, error){
filename := title + ".html"
body, err := ioutil.ReadFile(filename)
if err != nil{
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page){
t, err := template.ParseFiles(tmpl + ".html")
if err != nil{
panic(err)
}
err = t.Execute(w, p)
fmt.Println(err)
}
func response(c web.C, w http.ResponseWriter, r *http.Request){
p, err := loadPage("Projects/Go/src/web/site/index")
if err != nil{
p = &Page{Title: "I feel that is "}
panic(err)
}
renderTemplate(w, "Projects/Go/src/web/site/response", p)
}
func serveSingle(filename string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
}
}
func main() {
goji.Get("/", serveSingle("Projects/Go/src/web/site/index.html"))
goji.Handle("/ask", response)
goji.Serve()
}
答案1
得分: 0
你的loadPage()
函数默认将Page.Title
设置为文件路径,去掉.html
扩展名。
在你的response()
函数中,只有当err != nil
时,你才会覆盖这个默认行为。你还完全覆盖了p
变量,使用了p = &Page{Title: "I feel that is "}
这一行,而不仅仅是设置现有Page
的Title
字段。
你可以尝试修改如下:
func response(c web.C, w http.ResponseWriter, r *http.Request){
p, err := loadPage("Projects/Go/src/web/site/index")
if err != nil{
panic(err)
}
p.Title = "I feel that is "
renderTemplate(w, "Projects/Go/src/web/site/response", p)
}
这样修改后,当err != nil
时,只会抛出错误,而不会覆盖p
变量。然后,你可以直接设置p.Title
的值,并将其传递给renderTemplate()
函数。
英文:
Your loadPage()
function sets Page.Title
to the file path, minus the .html
extension by default.
You are only overriding this default behaviour in your response()
function when err != nil
. You're also completely overriding the p
variable with the line p = &Page{Title: "I feel that is "}
instead of just setting the Title
field on the existing Page
.
You should try changing:
func response(c web.C, w http.ResponseWriter, r *http.Request){
p, err := loadPage("Projects/Go/src/web/site/index")
if err != nil{
p = &Page{Title: "I feel that is "}
panic(err)
}
renderTemplate(w, "Projects/Go/src/web/site/response", p)
}
To:
func response(c web.C, w http.ResponseWriter, r *http.Request){
p, err := loadPage("Projects/Go/src/web/site/index")
if err != nil{
panic(err)
}
p.Title = "I feel that is "
renderTemplate(w, "Projects/Go/src/web/site/response", p)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论