Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates Go模板-从对象存储/数据库加载_Templates_Go_Mux_Gorilla - Fatal编程技术网

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

Templates Go模板-从对象存储/数据库加载,templates,go,mux,gorilla,Templates,Go,Mux,Gorilla,我正在重建一个应用程序,它支持从node.js到Go的特定于客户的模板(主题) 我目前正在使用渲染模板文件,但我实际需要做的是访问存储在对象存储中的模板,如Cloudfiles 在node.js中,我已经用express完成了这项工作,并且覆盖了render()方法,但是我还没有弄清楚如何在Go中完成这项工作 我基本上需要这样做: func (c *Controller) MyRouteHandler (rw http.ResponseWriter, req *http.Request) {

我正在重建一个应用程序,它支持从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})
}
如果需要的话,我可以通过这样做动态地包含子模板:但老实说,这似乎不是一个好方法

我一直在寻找解决这个问题的方法,但总是遇到障碍。任何建议/帮助都很好

编辑:

本质上,我的问题如下:

  • 如何根据每个请求从数据存储中加载特定模板
  • 然后我如何将其作为响应发送给客户机

  • 如何根据每个请求从数据存储中加载特定模板

    //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)
    
    然后我如何将其作为响应发送给客户机

    tmpl, err := template.New("name").Parse(templateString)
    tmpl.Execute(rw, &yourDataModel{})
    

    所以你真正的问题是找到一种从Cloudfiles加载文本的方法吗?不,我可以从Cloudfiles加载文件的内容。这一点很容易。它能够使用Gosorry将其呈现为模板,我仍然不知道您真正的问题。从我可以看出,默认情况下,Go的模板要求模板在编译时可用,您需要重新编译,以便模板更改生效。我需要从外部源(如数据库)加载模板,并在每次请求之前动态解析它们。当然,您可以在运行时加载Go的模板内容,只需“动态”读取模板内容并调用parse()函数。