英文:
Dynamically parsing files
问题
var templates = template.Must(template.ParseFiles(
"index.html", // 主文件
"subfolder/index.html", // 子文件夹中的同名文件会在运行时出错
"includes/header.html", "includes/footer.html",
))
func main() {
// 遍历并解析文件
filepath.Walk("files", func(path string, info os.FileInfo, err error) {
if !info.IsDir() {
// 将路径添加到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)
}
}
英文:
For parsing files i have setup a variable for template.ParseFiles and i currently have to manually set each file.
Two things:
How would i be able to walk through a main folder and a multitude of subfolders and automatically add them to ParseFiles so i dont have to manually add each file individually?
How would i be able to call a file with the same name in a subfolder because currently I get an error at runtime if i add same name file in ParseFiles.
<!-- language: lang-go -->
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)
}
}
答案1
得分: 4
要遍历一个目录查找文件,请参阅:http://golang.org/pkg/path/filepath/#Walk 或者 http://golang.org/pkg/html/template/#New 和 http://golang.org/pkg/html/template/#Template.Parse
至于你的另一个问题,ParseFiles 使用文件的基本名称作为模板名称,这导致模板发生冲突。你有两个选择:
- 重命名文件。
- 使用
t := template.New("你选择的名称")
创建一个初始模板- 使用你已经开始的遍历函数,并为每个模板文件调用
t.Parse("文件中的文本")
。你需要自己打开并读取模板文件的内容来传递给这里。
- 使用你已经开始的遍历函数,并为每个模板文件调用
编辑:代码示例。
func main() {
// 遍历和解析文件
t = template.New("我的模板名称")
filepath.Walk("files", func(path string, info os.FileInfo, err error) {
if !info.IsDir() {
// 将路径添加到 ParseFiles
content := ""
// 在这里将文件读取到 content 中
t.Parse(content)
}
return
})
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
英文:
To walk a directory looking for files see: http://golang.org/pkg/path/filepath/#Walk or http://golang.org/pkg/html/template/#New and http://golang.org/pkg/html/template/#Template.Parse
As for your other question ParseFiles uses the base name of the file as the template name which results in a collision in your template. You have two choices
- Rename the file.
- use
t := template.New("name of your choice")
to create an initial template - Use the walk function you already have started and call
t.Parse("text from file")
for each template file. You'll have to open and read the contents of the template files yourself to pass in here.
Edit: Code example.
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)
}
答案2
得分: 0
所以基本上我设置了New("我想要的路径名").Parse("从读取文件得到的字符串"),同时遍历文件夹。
var templates = template.New("temp")
func main() {
// 遍历并解析文件
parseFiles()
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
////////////////////////
// 处理函数 //
////////////////////////
func home(w http.ResponseWriter, r *http.Request) {
render(w, "index.html")
render(w, "subfolder/index.html")
}
//////////////////////////
// 可重用的函数 //
//////////////////////////
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)
}
}
英文:
So basically i set New("path name i want").Parse("String from read file") while walking through the folders.
<!-- language: lang-go -->
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论