// ParseFiles creates a new Template and parses the template definitions from// the named files. The returned template's name will have the (base) name and// (parsed) contents of the first file. There must be at least one file.// If an error occurs, parsing stops and the returned *Template is nil.//// When parsing multiple files with the same name in different directories,// the last one mentioned will be the one that results.// For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template// named "foo", while "a/foo" is unavailable.funcParseFiles(filenames ...string)(*Template,error){returnparseFiles(nil, filenames...)}
把模版信息响应写入到输出流中
// Execute applies a parsed template to the specified data object,// writing the output to wr.// If an error occurs executing the template or writing its output,// execution stops, but partial results may already have been written to// the output writer.// A template may be executed safely in parallel, although if parallel// executions share a Writer the output may be interleaved.func(t *Template)Execute(wr io.Writer, data interface{})error{if err := t.escape(); err !=nil{return err
}return t.text.Execute(wr, data)}
代码演示,显示index.html信息
因为配置的pattern为"/"所以资源路径任意,都可以访问到这个HTML
package main
import("net/http""html/template")funcwelcome(w http.ResponseWriter, r *http.Request){
t,_:= template.ParseFiles("view/index.html")
t.Execute(w,nil)//第二个参数表示向模版传递的数据}funcmain(){
server := http.Server{Addr:":8090"}
http.HandleFunc("/", welcome)
server.ListenAndServe()}
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Title</title><!--路径以斜杠开头,表示项目根目录下--><scripttype="text/javascript"src="/static/js/index.js"></script></head><body>
这是要显示的html页面信息<br/><buttononclick="myclick()">按钮</button></body></html>
index.js代码如下
functionmyclick(){alert("您点击了按钮")}
代码示例
package main
import("net/http""html/template")funcwelcome(w http.ResponseWriter, r *http.Request){
t,_:= template.ParseFiles("view/index.html")
t.Execute(w,nil)//第二个参数表示向模版传递的数据}funcmain(){
server := http.Server{Addr:":8090"}/*
访问url以/static/开头,就会把访问信息映射到指定的目录中
*/
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", welcome)
server.ListenAndServe()}