gin03模板语法下

自定义模板函数

首先我们自定义一个函数,根据时间戳返回时间的函数

func UnixToTime(timestamp int64) string{
t:=timestamp.Unix(timestamp,0)
return t.Format("2006-01-02 15:04:05")
}

然后我们将函数与路由绑定

r.SetFuncMap(template.FuncMap{
"UnixToTime":UnixToTime,
})

然后后台传入数据date

image

然后在html页面中调用

{{UnixToTime .date}}

定义模板文件以及加载静态资源

一个大型的网页肯定会有很多html页面,而且我们也不可能每个html页面都单独写一下

定义一个模板头部

{{ define "public/page_header.html" }}
<h1>我是头部</h1>
{{ end }}

定义一个模板尾部

{{define "public/page_footer.html"}}
<h1>我是尾部</h1>
{{end}}

使用模板

{{template "public/page_header.html" . }}

**注意上面的 点. **

加载静态资源如css,js,image

r.Static("/static","./static")//将我们当前目录下static绑定到static路由上

static/css/base.css

h1{
backgroud:#fff;
color:white;
text-align
}

然后引入该css

<link rel="stylesheet" href="/static/css/base.css">

这样就行了