Golang在等待加载数据时显示静态HTML模板

时间:2017-07-26 23:06:37

标签: http go dynamic go-html-template

我有一个路由设置,它使用动态HTML模板进行响应。

package main

import (
    "net/http"
    "html/template"
)

func index(w http.ResponseWriter, r *http.Request) {
    showWwResult, _ := GetWw()
    showHoursResult, _ := GetHours()

    type Data struct {
        ShowWwResult   []IssueResult
        ShowHoursResult Page
    }

    data := Data{showWwResult, showHoursResult}

    var templates = template.Must(template.ParseFiles("templates/index.html", "templates/ww.html", "templates/hours.html"))
    templates.ExecuteTemplate(w, "indexPage", data)
}

我的问题是,收集数据需要很长时间,而页面在呈现HTML之前会等待返回。

在等待GetWw()GetHours()完成时,如何让它返回某些,什么?有没有办法显示我的HTML模板的静态部分,然后在页面填充时使用ShowWwResultShowHoursResult填充页面?

1 个答案:

答案 0 :(得分:2)

不,最好的方法是提供模板然后使用ajax调用填充它,该端点返回带有您要使用的数据的json。