5

如何显示模板的内容?

包主

import (
    "fmt"
    "html/template"
    "os"

)

func main() {
    t := template.New("another")
    t,e:=t.ParseFiles("test.html")
    if(e!=nil){
            fmt.Println(e);
    }
    t.Execute(os.Stdout, nil)

}

为什么不呢?test.html 存在

4

1 回答 1

7

您无需创建新模板NewParseFiles在其上使用。还有一个功能ParseFiles可以在幕后创建新模板。
这是一个例子:

package main

import (
    "fmt"
    "html/template"
    "os"
)

func main() {
    t, err := template.ParseFiles("test.html")
    if err != nil {
            fmt.Println(err);
    }
    t.Execute(os.Stdout, nil)
}
于 2012-04-17T07:34:58.400 回答