嵌入 FS 无法正确解析模板

我有以下代码:


package main


import (

    "fmt"

    "html/template"

    "log"

    "net/http"

    "onsen/resources"

)


var view *template.Template

var err error


func init() {

    fmt.Println("Starting up.")

    view = template.Must(template.ParseFS(resources.Views, "templates/layouts/*.html", "templates/views/*.html", "templates/partials/*.html"))

    if err != nil {

        log.Fatal("Error loading templates:" + err.Error())

    }

}


func main() {

    server := http.Server{

        Addr: "127.0.0.1:8070",

    }


    http.Handle("/webUI/", http.StripPrefix("/webUI/", http.FileServer(http.FS(resources.WebUI))))


    http.HandleFunc("/process", process)

    http.HandleFunc("/home", home)

    http.HandleFunc("/test", test)


    server.ListenAndServe()

}

在哪里onsen/resources:


package resources


import (

    "embed"

)


// Views is our static web server layouts, views with dynamic content and partials content that is a static view.

//go:embed templates/layouts templates/views templates/partials

var Views embed.FS

路线功能是:


package main


import (

    "log"

    "net/http"

)


func home(w http.ResponseWriter, r *http.Request) {

    err = view.ExecuteTemplate(w, "home.html", nil)

    if err != nil {

        log.Fatalln(err)

    }

}


func test(w http.ResponseWriter, r *http.Request) {

    err = view.ExecuteTemplate(w, "test.html", nil)

    if err != nil {

        log.Fatalln(err)

    }

}


func process(w http.ResponseWriter, r *http.Request) {

    err = view.ExecuteTemplate(w, "process.html", nil)

    if err != nil {

        log.Fatalln(err)

    }

}

我的模板是: base.html


<!-- Content of base.html: -->

{{define "base"}}

<!doctype html>

 <html>

 <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <meta charset="utf-8">

 </head>

 <body>

            {{template "content" .}}

</body>

</html>

{{end}}

意见是:


<!-- Content of home.html: -->

{{template "base" .}}

{{define "content"}}

This is home

{{end}}


但是所有路线都显示相同的结果,与模板相同test.html

http://img3.mukewang.com/6343cdf10001524612690235.jpg

我为我的模板结构引用了这个,但看起来与嵌入有关!



PIPIONE
浏览 63回答 1
1回答

慕斯王

应用程序将所有模板文件解析为一个模板集。test.html 中的“内容”模板是要添加到集合中的最后一个“内容”模板。这就是你在每一页看到的那个。将每个页面的模板解析为单独的集合。所以,这里是正确的工作代码package mainimport (&nbsp; &nbsp; "html/template"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "onsen/resources")func process(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; view := template.Must(template.ParseFS(resources.Views, "templates/layouts/base.html", "templates/views/other.html", "templates/partials/*.html"))&nbsp; &nbsp; type approval struct {&nbsp; &nbsp; &nbsp; &nbsp; Status bool&nbsp; &nbsp; }&nbsp; &nbsp; workflow := approval{Status: false}&nbsp; &nbsp; err = view.ExecuteTemplate(w, "process.html", workflow)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatalln(err)&nbsp; &nbsp; }}模板process.html是:<!-- Content of other.html: -->{{template "base" .}}{{define "content"}}process goes here&nbsp;{{ .Status }}&nbsp; &nbsp; &nbsp;{{if .Status}}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {{template "approved"}}&nbsp; &nbsp; {{else}}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {{template "rejected"}}&nbsp; &nbsp; {{end}}{{end}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go