dsf4s5787 2014-08-25 01:51
浏览 93
已采纳

html模板中的内容已替换为模板的文件位置,而不是所需的文本

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条回答 默认 最新

  • dragon456101 2014-08-26 11:25
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 罗技G293和UE5.3
  • ¥20 Tesla 特斯拉K80显卡 如果需要使用该设备,你需要禁用系统上的另一个设备。
  • ¥30 QT调用百度智能云千帆模型无法取得返回文本
  • ¥50 CCD工业视觉相机检测出现光边
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现
  • ¥50 python自动地图截图脚本
  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上