dran0703 2015-09-10 13:27
浏览 33
已采纳

将一个模板渲染到另一个模板中,而无需每次都对其进行解析

I have three templates like this:

base.html:

<h1>Base.html rendered here</h1>
{{template "content" .}}

view.html:

{{define "content"}}
...
{{end}}

edit.html:

{{define "content"}}
...
{{end}}

I store them in folder "templates".

What i want is to dynamically change template which will be rendered in {{template "content" .}} place, without parsing every time. So what i DO NOT want is this :

func main() {
   http.HandleFunc("/edit", handlerEdit)
   http.HandleFunc("/view", handlerView)
   http.ListenAndServe(":8080", nil)
}
func handlerView(w http.ResponseWriter, req *http.Request) {
   renderTemplate(w, req, "view")
}

func handlerEdit(w http.ResponseWriter, req *http.Request) {
   renderTemplate(w, req, "edit")
}

func renderTemplate(w http.ResponseWriter, req *http.Request, tmpl    string) {
   templates, err := template.ParseFiles("templates/base.html",  "templates/"+tmpl+".html")
   if err != nil {
       fmt.Println("Something goes wrong ", err)
       return
   }
   someData := &Page{Title: "QWE", Body: []byte("sample body")}
   templates.Execute(w, someData)
}

I was looking at the template.ParseGlobe(), in order to do something like this

var templates = template.Must(template.ParseGlob("templates/*.html"))
... //and then somthing like this:
err := templates.ExecuteTemplate(w, tmpl+".html", p)

But ExecuteTamplate() recieves only one string as template's name. How in this case i can render two and more templates?

  • 写回答

1条回答 默认 最新

  • duanbei1598 2015-09-10 19:51
    关注

    Instead of writing directly to the http.ResponseWriter on your call to ExecuteTemplate, write to a byte buffer and send that through the call to the next template by prepping it with a template.HTML call.

    var b bytes.Buffer
    
    var templates = template.Must(template.ParseGlob("templates/*.html"))
    
    err := templates.ExecuteTemplate(b, templ_1, p)
    if err != nil { //handle err }
    err := templates.ExecuteTemplate(w, templ_2, template.HTML(b.String()))
    if err != nil { //handle err }
    

    If you're going to use an unknown number of templates, you can capture the intermediate step with a string:

    var strtmp string
    err := templates.ExecuteTemplate(b, templ_1, p)
    if err != nil { //handle err }
    
    strtemp = b.String()  //store the output
    b.Reset()             //prep buffer for next template's output
    
    err := templates.ExecuteTemplate(b, templ_2, template.HTML(strtmp))
    if err != nil { //handle err }
    
    //... until all templates are applied
    
    b.WriteTo(w)  //Send the final output to the ResponseWriter
    

    EDIT: As @Zhuharev pointed out, if the composition of the view and edit templates are fixed, they can both reference base rather than base trying to provide a reference to either view or edit:

    {{define "viewContent"}}
    {{template "templates/base.html" .}}
    ...Current view.html template...
    {{end}}
    
    {{define "editContent"}}
    {{template "templates/base.html" .}}
    ...Current edit.html template...
    {{end}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!