Go 模板 - 从对象存储/数据库加载

我正在重建一个支持客户特定模板(主题)的应用程序,从 node.js 到 Go。


我目前正在使用渲染来渲染我的模板文件,但我实际上需要做的是访问存储在对象存储(如 Cloudfiles)中的模板。


在 node.js 中,我使用 express 完成了此操作,并且正在覆盖该render()方法,但我无法弄清楚如何在 Go 中执行此操作。


我基本上需要做这样的事情:


func (c *Controller) MyRouteHandler (rw http.ResponseWriter, req *http.Request) {

    // retrieve the store from the context (assigned in middleware chain)

    store := context.Get(req, "store").(*Store) 


    ... do some stuff like load the entity from the database


    // retrieve the template from the Object store and 

    // create the template instance (template.New("template").Parse(...))

    tpl := c.ObjectStore.LoadTemplate(store, entity.TemplateFile)


    // I know render's .HTML function takes the path to the template 

    // so I'll probably need to show the html a different way

    c.HTML(rw, http.StatusOK, tpl, &PageContext{Title: "My Page", Entity: &entity})

}

如果需要,我可以通过执行以下操作来动态包含子模板:http : //play.golang.org/p/7BCPHdKRi2但老实说,这似乎不是一个好方法。


我一直在寻找解决方案,但一直遇到障碍。任何建议/帮助都会很棒。


编辑:


本质上,我要问以下问题:


如何基于每个请求从数据存储加载特定模板。

然后我如何将其作为对客户端的响应发送


慕码人8056858
浏览 148回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP