Golang 在等待数据加载时显示静态 HTML 模板
Golang display static HTML template while waiting for data to load
我设置了一个使用动态 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)
}
我的问题是,收集数据需要很长时间,页面会等待 return,然后呈现 HTML。
在等待 GetWw()
和 GetHours()
完成的同时,我怎样才能将它变成 return 某事 ,任何事?有什么方法可以显示我的 HTML 模板的静态部分,然后在 ShowWwResult
和 ShowHoursResult
准备好后用它们填充页面?
不,最好的方法是提供模板,然后使用 ajax 调用端点填充模板,returns json 使用您要使用的数据。
我设置了一个使用动态 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)
}
我的问题是,收集数据需要很长时间,页面会等待 return,然后呈现 HTML。
在等待 GetWw()
和 GetHours()
完成的同时,我怎样才能将它变成 return 某事 ,任何事?有什么方法可以显示我的 HTML 模板的静态部分,然后在 ShowWwResult
和 ShowHoursResult
准备好后用它们填充页面?
不,最好的方法是提供模板,然后使用 ajax 调用端点填充模板,returns json 使用您要使用的数据。