我正在尝试在Go lang中生成网页。我目前正在使用Goji框架(http://goji.io),并且想要生成网页主体的所有头部和部分,但是随后我希望根据代码的结果。
例如,在PHP中,可以编写HTML,js或CSS,然后在PHP标记中编写在那里解释的代码。
如何编写我的html,css和js,然后在其中呈现页面时编译并执行Golang代码?
如第13期所述,请使用Go html / template包。
即
// Shorthand
type M map[string]interface{}
func viewHandler(c web.C, w http.ResponseWriter, r *http.Request) {
title := c.URLParams["title"]
p, err := loadPage(title)
if err != nil {
...
}
// do other things
template.ExecuteTemplate(w, "results.html", M{
"title": title,
"results": results,
"pagination": true,
}
}
results.html
{{range .Results }}
<h1>{{ Result.Name }}</h1>
<p>{{ Result.Body }}</p>
{{ end }}
在中也建议使用模板zenazn/goji/example/main.go
。
elithrar还在注释中引用了“ 编写Web应用程序文章,html / template程序包 ”部分,以获取更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句