使用嵌套模板的Golang意外EOF

时间:2017-10-27 10:27:38

标签: templates go go-templates

我正在尝试在页面上显示切片的内容。使用静态文本显示模板有效。一旦我尝试将范围超过发送到ExTpl()的切片,我就会收到EOF错误。

type Miner struct {
    IP          string `bson:"ip"`
    Port        int32  `bson:"port"`
    FailCount   int    `bson:"failcount"`
}

type (
    MinerController struct {
        Session *mgo.Session
    }
)

func ExTpl(w http.ResponseWriter, data []Miner) {
    t, _ := template.ParseFiles("templates/header.tmpl", "templates/footer.tmpl", "templates/data.tmpl")
    t.ExecuteTemplate(w, "data", data)
}

func FilterMiners(c *mgo.Collection, key, value string, limit int) ([]Miner, int, error) {
    var results []Miner
    query := bson.M{key: value}
    if len(key) == 0 {
        query = nil
    }

    err := c.Find(query).Limit(limit).All(&results)
    if err != nil {
        return results, 0,errors.New("error retrieving hosts")
    }
    return results, len(results), nil
}

func (mc MinerController) GetSpecificMiners(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    l, err := strconv.Atoi(vars["limit"])
    if err != nil {
        l = 0
    }

    collection := Collection("miners", "NASty", mc.Session)
    fleet, _ , _ := FilterMiners(collection, vars["key"], vars["value"], l)
    ExTpl(w, fleet)
}

以下是相关模板:

header.tmpl

{{define "header"}}
<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
  </head>
  <body>
{{end}}

data.tmpl

{{define "data"}}
{{template "header"}}
{{range .}}
    {{.IP}}
{{template "footer"}}
{{end}}

footer.tmpl

{{define "footer"}}
</body>
</html>
{{end}}

当我运行它时,我收到以下错误:

2017/10/27 12:24:56 http: panic serving [::1]:50252: template: data.tmpl:6: unexpected EOF
goroutine 49 [running]:
net/http.(*conn).serve.func1(0xc042138000)
    C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x7abbc0, 0xc042170360)
    C:/Go/src/runtime/panic.go:489 +0x2dd
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers.ExTpl(0x9b7820, 0xc04211c460, 0xc042172000, 0x2, 0x2)
    D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers/handlers.go:25 +0x84
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers.MinerController.GetSpecificMiners(0xc04203b040, 0x9b7820, 0xc04211c460, 0xc0420f6600)
    D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers/miners.go:74 +0x2b9
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers.(MinerController).GetSpecificMiners-fm(0x9b7820, 0xc04211c460, 0xc0420f6600)
    D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/routes/routes.go:33 +0x55
net/http.HandlerFunc.ServeHTTP(0xc04203f210, 0x9b7820, 0xc04211c460, 0xc0420f6600)
    C:/Go/src/net/http/server.go:1942 +0x4b
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/routes.Logger.func1(0x9b7820, 0xc04211c460, 0xc0420f6600)
    D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/routes/handlers.go:12 +0x18d
net/http.HandlerFunc.ServeHTTP(0xc0420467c0, 0x9b7820, 0xc04211c460, 0xc0420f6600)
    C:/Go/src/net/http/server.go:1942 +0x4b
github.com/gorilla/mux.(*Router).ServeHTTP(0xc04203c420, 0x9b7820, 0xc04211c460, 0xc0420f6600)
    D:/_projects/Go/src/github.com/gorilla/mux/mux.go:133 +0x108
net/http.serverHandler.ServeHTTP(0xc04208e2c0, 0x9b7820, 0xc04211c460, 0xc042116100)
    C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc042138000, 0x9b7fe0, 0xc042104280)
    C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
    C:/Go/src/net/http/server.go:2668 +0x2d5

我不确定发生了什么,因为我的所有模板都有正确的格式,Miner结构包含IP字段,我应该能够在范围循环中访问与.IP

2 个答案:

答案 0 :(得分:3)

您的data模板定义无效。 {{define}}{{range}}操作都需要关闭{{end}},因此应该是:

{{define "data"}}
    {{template "header"}}
    {{range .}}
        {{.IP}}
    {{end}}
    {{template "footer"}}
{{end}}

一些建议:

答案 1 :(得分:2)

从我所看到的,你在data.tmpl中找不到{{end}}。

{{define "data"}}
{{template "header"}}
{{range .}}
    {{.IP}}
{{template "footer"}}
{{end}}

应该是:

{{define "data"}}
{{template "header"}}
{{range .}}
    {{.IP}}
{{end}}
{{template "footer"}}
{{end}}