英文:
Go Templates - Loading from an object store / Database
问题
我正在将一个支持客户特定模板(主题)的应用程序从Node.js重构为Go。
目前我正在使用render来渲染我的模板文件,但实际上我需要访问存储在对象存储中(如Cloudfiles)的模板。
在Node.js中,我使用express来实现这个功能,并且我重写了render()
方法,但是我还没有找到如何在Go中实现这个功能。
我基本上需要做这样的事情:
func (c *Controller) MyRouteHandler(rw http.ResponseWriter, req *http.Request) {
// 从上下文中检索存储(在中间件链中分配)
store := context.Get(req, "store").(*Store)
... 从数据库加载实体等一些操作
// 从对象存储中检索模板并创建模板实例(template.New("template").Parse(...))
tpl := c.ObjectStore.LoadTemplate(store, entity.TemplateFile)
// 我知道render的.HTML函数需要模板的路径,所以我可能需要以不同的方式显示HTML
c.HTML(rw, http.StatusOK, tpl, &PageContext{Title: "My Page", Entity: &entity})
}
如果需要,我可以通过以下方式动态包含子模板:http://play.golang.org/p/7BCPHdKRi2,但老实说,这似乎不是一个很好的方法。
我已经搜索了解决这个问题的方法,但一直遇到障碍。任何建议/帮助将是非常好的。
编辑:
实际上,我正在询问以下问题:
- 如何在每个请求的基础上从数据存储加载特定的模板。
- 如何将其作为响应发送给客户端。
英文:
I'm rebuilding an app that supports customer specific templating (themes) from node.js to Go.
I'm currently using render to render my template files but what I actually need to do access templates that are stored in an object store such as Cloudfiles.
In node.js I've done this with express and I'm overriding the render()
method but I've not been able to figure out how to do this in Go.
I essentially need to do something like this:
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})
}
I can dynamically include sub-templates by doing something like this if needed: http://play.golang.org/p/7BCPHdKRi2 but it doesn't seem like a great way if I'm honest.
I've searched for a solution to this but keep hitting road blocks. Any advice / assistance would be great.
Edit:
In essence, I'm asking the following:
- How can I load a specific template from a datastore on a per-request basis.
- How can I then send that as a response to the client
答案1
得分: 0
如何在每个请求的基础上从数据存储加载特定模板。
//以HTTP为例:
resp, err := http.Get("http://mytemplates.com/template1")
if err != nil {
//处理错误
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
templateString := string(body)
然后如何将其作为响应发送给客户端
tmpl, err := template.New("name").Parse(templateString)
tmpl.Execute(rw, &yourDataModel{})
英文:
How can I load a specific template from a datastore on a per-request basis.
//take HTTP for example:
resp, err := http.Get("http://mytemplates.com/template1")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
templateString := string(body)
How can I then send that as a response to the client
tmpl, err := template.New("name").Parse(templateString)
tmpl.Execute(rw, &yourDataModel{})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论