这是一个Example https://www.dropbox.com/sh/ur2ws1jnik6euef/PjVJSwDTUc/Blog-Golang.zip该应用程序的。关键代码在:golang-code/handler/handler.go(主题后面应该出现一个ID!)
我正在尝试在 Google Appengine 上用 Golang 构建一个小型博客系统,并使用 Mustache 作为模板引擎。
所以,我有一个结构:
type Blogposts struct {
PostTitle string
PostPreview string
Content string
Creator string
Date time.Time
}
数据通过以下方式传递给 GAE
datastore.Put(c, datastore.NewIncompleteKey(c, "Blogposts", nil), &blogposts)
因此,GAE 自动分配一个 intID (int64)。
现在我尝试获取最新的博客文章
// Get the latest blogposts
c := appengine.NewContext(r)
q := datastore.NewQuery("Blogposts").Order("-Date").Limit(10)
var blogposts []Blogposts
_, err := q.GetAll(c, &blogposts)
直到一切正常,但是当我尝试访问 intID (或 stringID,等等)时,我无法访问这个:-(
<h3><a href="/blog/read/{{{intID}}}">{{{PostTitle}}}</a></h3>
(PostTitle 有效,intID 无效,我尝试了数千种方法,但没有任何效果:-( )
有人有想法吗?这太好了!
编辑:
我用小胡子。
http://mustache.github.com/ http://mustache.github.com/
在我使用的代码中:
x["Blogposts"] = blogposts
data := mustache.RenderFile("templates/about.mustache", x)
sendData(w, data) // Equivalent to fmt.Fprintf
然后可以使用 {{{Content}}} 或 {{{PostTitle}}} 等在 .mustache 模板中访问数据。