第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

html 模板中的內(nèi)容被模板的文件位置替換,而不是所需的文本

html 模板中的內(nèi)容被模板的文件位置替換,而不是所需的文本

Go
BIG陽 2021-08-10 20:31:01
我正在使用 html/template 包在提交表單時(shí)提供模板。作為該模板副本的頁面正在使用模板文件的位置而不是應(yīng)替換 {{ .Title }} 的文本呈現(xiàn)所以在 response.html 中,{{ .Title }} 顯示為“Projects/Go/src/web/site/index”而不是“我覺得是”如何將 {{ .Title }} 替換為文本而不是文件位置?這是我的代碼:package mainimport (    "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 回答

?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

您的loadPage()函數(shù)設(shè)置Page.Title為文件路徑,.html默認(rèn)情況下減去擴(kuò)展名。


您只覆蓋在你的默認(rèn)行為response()功能時(shí)err != nil。您還可以p使用該行完全覆蓋變量,p = &Page{Title: "I feel that is "}而不僅僅是Title在現(xiàn)有Page.


你應(yīng)該嘗試改變:


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

}


查看完整回答
反對 回復(fù) 2021-08-10
  • 1 回答
  • 0 關(guān)注
  • 262 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號