欢迎光临
我们一直在努力

go语言学习例子No.9–web模板的使用

在Go语言中web项目标准结构如下

--项目名
    --src
    --static
        --css
        --images
        --js
    --view //这里放模板
        --login
           --login.html
        --index.html
    --main.go

模板使用

package main
import (
	"html/template"
	"net/http"
)
func login(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/login/login.html")//找模板
	t.Execute(w, nil)//执行模板
}
func main() {
	server := http.Server{Addr: "localhost:8080"}
	http.HandleFunc("/login", login)
	server.ListenAndServe()
}

这样写的话静态文件都比如css,js或者图片都找不到,要加这样一段代码

package main
import (
	"html/template"
	"net/http"
)
func login(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/login/login.html")
	t.Execute(w, nil)

}
func main() {
	server := http.Server{Addr: "localhost:8080"}
	/*
	访问url以/static/开头,就会把访问信息映射到指定的目录中
	 */
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/login", login)
	server.ListenAndServe()
}

这样就可以解析static里面的静态文件了

模板传参

#index.html

<!DOCTYPE html>
<html>
<head>
    <title>index</title>
</head>
<body>
<pre>
当前用户信息:<br/>
    姓名:{{.}}<br/>
</pre>
</body>
</html>

template.Execute()第二个参数传递的值

最常用的{{.}}中的”.”是指针,指向当前变量,称为”dot”

package main
import (
	"html/template"
	"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/index.html")
	t.Execute(w, "李四")//
}
func main() {
	server := http.Server{Addr: "localhost:8080"}
	http.HandleFunc("/", index)
	server.ListenAndServe()
}

传递结构体类型数据

<!DOCTYPE html>
<html>
<head>
    <title>index</title>
</head>
<body>
<pre>
当前用户信息:<br/>
    姓名:{{.Name}}<br/>
    年龄:{{.Age}}<br/>
</pre>
</body>
</html>
package main
import (
	"html/template"
	"net/http"
)
//注意:只有首字母大写的属性才能在模版中访问到
type User struct {
	Name string
	Age  int
}
func index(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/index.html")
	t.Execute(w, User{"李四", 31})//此处传递结构体数据
}
func main() {
	server := http.Server{Addr: "localhost:8080"}
	http.HandleFunc("/", index)
	server.ListenAndServe()
}

向模版传递map类型数据

HTML代码如下,里面使用了连缀写法

<!DOCTYPE html>
<html>
<head>
    <title>index</title>
</head>
<body>
<pre>
当前用户信息:<br/>
    姓名:{{.user.Name}}<br/>
    年龄:{{.user.Age}}<br/>
    购物金额:{{.money}}
</pre>
</body>
</html>
package main
import (
	"html/template"
	"net/http"
)
type User struct {
	Name string
	Age  int
}
func index(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/index.html")
	m := make(map[string]interface{})
	m["user"] = User{"李四", 31}
	m["money"] = 1000
	t.Execute(w, m)//此处传递map数据
}
func main() {
	server := http.Server{Addr: "localhost:8080"}
	http.HandleFunc("/", index)
	server.ListenAndServe()
}

模板调用函数

package main
import (
	"html/template"
	"net/http"
	"time"
)
type User struct {
	Name string
	Age  int
}
func MyFormat(s string) string {
	t, _ := time.Parse("2006-01-02 15:04:05", s)
	t = t.Add(60e9) //在时间上添加1分钟
	return t.Format("2006-01-02 15:04:05")
}
func index(w http.ResponseWriter, r *http.Request) {
	//把自定义函数绑定到FuncMap上
	funcMap := template.FuncMap{"mf": MyFormat}
	//此处注意,一定要先绑定函数
	t := template.New("index.html").Funcs(funcMap)
	//绑定函数后在解析模版
	t, _ = t.ParseFiles("view/index.html")
	s := "2009-08-07 01:02:03"
	t.Execute(w, s)
}
func main() {
	server := http.Server{Addr: "localhost:8080"}
	http.HandleFunc("/", index)
	server.ListenAndServe()
}
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:家里蹲的狐狸 » go语言学习例子No.9–web模板的使用

分享到: 生成海报
avatar

热门文章

  • 评论 抢沙发

    • QQ号
    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址

    登录

    忘记密码 ?

    切换登录

    注册

    我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活