2

ファイルを解析するために、template.ParseFiles の変数を設定しました。現在、各ファイルを手動で設定する必要があります。

2つのこと:

各ファイルを個別に手動で追加する必要がないように、メイン フォルダーと多数のサブフォルダーを調べて自動的に ParseFiles に追加するにはどうすればよいでしょうか?

現在、ParseFiles に同じ名前のファイルを追加すると実行時にエラーが発生するため、サブフォルダーで同じ名前のファイルを呼び出すにはどうすればよいでしょうか。

var templates = template.Must(template.ParseFiles(
    "index.html", // main file
    "subfolder/index.html" // subfolder with same filename errors on runtime
    "includes/header.html", "includes/footer.html",
))


func main() {
    // Walk and ParseFiles
    filepath.Walk("files", func(path string, info os.FileInfo, err error) {
        if !info.IsDir() {
            // Add path to ParseFiles


        }
        return
    })

    http.HandleFunc("/", home)
    http.ListenAndServe(":8080", nil)
}

func home(w http.ResponseWriter, r *http.Request) {
    render(w, "index.html")
}

func render(w http.ResponseWriter, tmpl string) {
    err := templates.ExecuteTemplate(w, tmpl, nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
4

2 に答える 2

4

ディレクトリをたどってファイルを探すには、http: //golang.org/pkg/path/filepath/#Walkまたはhttp://golang.org/pkg/html/template/#Newおよびhttp://golang.orgを参照してください。 /pkg/html/template/#Template.Parse

他の質問については、ParseFiles はファイルのベース名をテンプレート名として使用するため、テンプレートで競合が発生します。あなたには2つの選択肢があります

  1. ファイルの名前を変更します。
  2. t := template.New("name of your choice")初期テンプレートの作成に 使用
    1. 既に開始している walk 関数を使用して、t.Parse("text from file")テンプレート ファイルごとに呼び出します。ここで渡すには、自分でテンプレート ファイルの内容を開いて読み取る必要があります。

編集:コード例。

func main() {
    // Walk and ParseFiles
    t = template.New("my template name")
    filepath.Walk("files", func(path string, info os.FileInfo, err error) {
        if !info.IsDir() {
            // Add path to ParseFiles
            content := ""
            // read the file into content here
            t.Parse(content)
        }
        return
    })

    http.HandleFunc("/", home)
    http.ListenAndServe(":8080", nil)
}
于 2012-10-09T19:40:23.337 に答える
0

したがって、基本的には、フォルダを歩きながら New("path name i want").Parse("String from read file") を設定します。

var templates = template.New("temp")

func main() {
    // Walk and ParseFiles
    parseFiles()

    http.HandleFunc("/", home)
    http.ListenAndServe(":8080", nil)
}

//////////////////////
// Handle Functions //
//////////////////////
func home(w http.ResponseWriter, r *http.Request) {
    render(w, "index.html")
    render(w, "subfolder/index.html")
}

////////////////////////
// Reusable functions //
////////////////////////
func parseFiles() {
    filepath.Walk("files", func(path string, info os.FileInfo, err error) error {
        if !info.IsDir() {
            filetext, err := ioutil.ReadFile(path)
        if err != nil {
                return err
        }
        text := string(filetext)
            templates.New(path).Parse(text)
        }
        return nil
    })
}

func render(w http.ResponseWriter, tmpl string) {
    err := templates.ExecuteTemplate(w, tmpl, nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
于 2012-10-10T16:53:00.020 に答える