簡體   English   中英

模板解析錯誤-“不允許操作”

[英]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/t1templates/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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM