HTML模板中的内容被模板文件的位置替换,而不是所需的文本。

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

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 "}这一行,而不仅仅是设置现有PageTitle字段。

你可以尝试修改如下:

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

huangapple
  • 本文由 发表于 2014年8月25日 09:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/25478053.html
匿名

发表评论

匿名网友

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

确定