[英]template parsing error - “operation not permitted”
我正在开发Google App Engine Go应用程序,需要在我的一个软件包中使用一些HTML模板。 当前文件结构为:
GOPATH/github.com/NAME/PROJECT/
app/
app.go
app.yaml
package/
package.go
Templates/
Template.html
为了包括该软件包,我使用:
导入“ github.com/NAME/PROJECT/package”
在package.go内部,我尝试以各种方式解析我的Template.html文件:
//Template, err := template.ParseFiles("package/Templates/Template.html") //doesn't work - "The system cannot find the path specified."
//Template, err := template.ParseFiles("github.com/NAME/PROJECT/package/Templates/Template.html") //doesn't work - "The system cannot find the path specified."
//Template, err := template.ParseFiles("Templates/Template.html") //doesn't work - "The system cannot find the path specified."
//Template, err := template.ParseFiles("/Templates/Template.html") //doesn't work - "The system cannot find the path specified."
Template, err := template.ParseFiles("../package/Templates/Template.html") //works on desktop!
因此,我采用了适用于我的桌面测试环境的最后一个选项,将其上传到AppEngine,然后收到新错误“不允许操作” ...
如何解析具有上述文件配置(可在App Engine和台式机上使用)的HTML模板?
您需要在应用程序的根目录下有app.yaml
。 App Engine使用app.yaml
的位置来找出与您的应用程序关联的文件。 您想将此文件移到顶层。
例如,假设我们有类似的东西:
app.go
app.yaml
templates/t1
templates/t2
app.yaml通常是您的应用程序所需要的,而app.go是:
package app
import (
"html/template"
"net/http"
)
var templates = template.Must(template.ParseGlob("templates/*"))
func init() {
http.HandleFunc("/", rootHandler)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:] // drop the leading slash
tmpl := templates.Lookup(name)
if tmpl == nil {
http.NotFound(w, r)
return
}
tmpl.Execute(w, nil)
}
templates/t1
和templates/t2
是合适的模板文件。 然后,一旦有了这个,我们就可以在生成的Web应用程序中访问t1/
和t2/
,它应该可以在App Engine上很好地服务和部署。
关键是在应用程序的顶级目录中包含app.yaml
。 需要牢记的另一项警告:确保您尝试从动态应用程序读取的任何文件都不会被静态提供或跳过。 检查您的app.yaml
。 如果要静态提供文件,则通常只允许前端查看文件,这意味着您的后端将无法看到文件。 在部署过程中,跳过的文件将被完全忽略。
您无法从GAE中的目录中读取。 您可以将模板嵌入go文件中,也可以使用google Blob存储API。 https://developers.google.com/appengine/docs/go/blobstore/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.