package main

import (
"fmt"
"html/template"
"net/http"
) type User struct {
UserName string
Age int
} func info(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("F:\\GoDevelopment\\src\\gocode\\project01\\templates\\info.html")
if err != nil {
fmt.Println("open html file failed", err)
return
}
//data := "80天环游世界"
user := User{
"豪杰",
18,
}
t.Execute(w, user)
} func main() {
http.HandleFunc("/", info)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
panic("start http server failed,err")
}
}
html模板if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的个人中心</title>
</head>
<body>
<div>
<ul>
<li>《linux内核》</li>
<li>姓名{{.UserName}}</li>
<li>年龄{{.Age}}</li>
</ul>
{{/* go模板中的注释*/}}
{{ $age :=.Age}}
{{ $age }}
{{if gt .Age 18}}
<div>
"澳门首家赌场开业啦"
</div>
{{ else }}
<div>
<div>
快乐成长
</div>
</div>
{{endif}}
</div>
</body>
</html>
模板for循环
package main

import (
"fmt"
"html/template"
"net/http"
) type User struct {
UserName string
Age int
} func info(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("F:\\GoDevelopment\\src\\gocode\\project01\\templates\\info.html")
if err != nil {
fmt.Println("open html file failed", err)
return
}
//data := "80天环游世界"
userMap := map[int]User{
1:{"托尼",11},
2:{"天哥",12},
3:{"杰少",19},
}
t.Execute(w, userMap)
} func main() {
http.HandleFunc("/", info)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
panic("start http server failed,err")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的个人中心</title>
</head>
<body>
<div>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
{{ range $index,$user:= .}}
<tr>
<td>{{$index}}</td>
<td>{{$user.UserName}}</td>
<td>{{$user.Age}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</body>
</html>
模板函数
<p>map 长度: {{len .}}</p>
<p>{{index . 1}}</p>
{{with index . 1}}<p>
{{ printf "姓名:%s 年龄:%d" .UserName .Age}}</p>
{{end}}
自定添加模板方法
package main

import (
"html/template"
"io/ioutil"
"net/http"
) type User struct {
UserName string
Age int
} func info(w http.ResponseWriter, r *http.Request) {
//添加自定义的方法要在parse模板文件之前添加
kuaFunc := func(arg string) (string, error) {
return arg + "真帅", nil
}
htmlByte, err := ioutil.ReadFile("F:\\GoDevelopment\\src\\gocode\\project01\\templates\\info.html")
if err != nil {
return
}
//template.New("info") 创建一个Templates对象
templ, err := template.New("info").Funcs(template.FuncMap{"kua": kuaFunc}).Parse(string(htmlByte))+
if err != nil {
return
}
//data := "80天环游世界"
userMap := map[int]User{
1: {"托尼", 11},
2: {"天哥", 12},
3: {"杰少", 19},
}
templ.Execute(w, userMap)
} func main() {
http.HandleFunc("/", info)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
panic("start http server failed,err")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的个人中心</title>
</head>
<body>
<div>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
{{ range $index,$user:= .}}
<tr>
<td>{{$index}}</td>
<td>{{$user.UserName}}</td>
<td>{{$user.Age}}</td>
</tr>
{{end}}
</tbody>
</table>
<p>map 长度: {{len .}}</p>
<p>{{index . 1}}</p>
{{with index . 1}}
<p>{{ printf "姓名:%s 年龄:%d" .UserName .Age }}</p>
<p>{{ kua .UserName}}</p>
{{end}}
</div>
</body>
</html>

template方法

{{ template "ol.html"}} //在当前模板调用另外一个模板
{{ define "ol.html"}} //在当前模板中定义了另外一个模板,这里是模板命名

最新文章

  1. Spring注解学习
  2. 4. javacript高级程序设计-变量、作用域和内存问题
  3. 【DPM】Deformable Part Models matlab代码在windows下的调试过程
  4. 【python】datetime获取日期,前一天日期
  5. Open vSwitch安装及配置
  6. 分享一个MVC的多层架构,欢迎大家拍砖斧正
  7. onclick跳转
  8. jquery 父页面 子页面 同级页面 调用
  9. 自己动手写谷歌API翻译接口
  10. 【Android Developers Training】 41. 向另一台设备发送文件
  11. Java的字符串分割的不同实现
  12. python之sqlalchemy的使用
  13. Epemme
  14. Ubuntu 18.04 Server上安装LAMP
  15. table tr 加入背景色之后 去掉td之间的空隙
  16. 如何将指定文件或文件夹直接提交到svn指定目录
  17. neo4j----创建索引
  18. eth0 eth0:1 eth0.1 的区别
  19. Scalable IO in Java【java高效IO】
  20. 微信小程序退款 处理类

热门文章

  1. bzoj 4500 矩阵 题解
  2. java笔记——java加载与执行
  3. fastjson转换对象时出错,&quot;$ref&quot;: &quot;$.data.list[0].xxxx&quot;
  4. 【C#】上机实验七
  5. Oracle中的dual表简介
  6. 解决springboot 新版本 2.1.6 spring-boot-starter-actuator 访问报404
  7. Harbor 企业级私有仓库 Ubuntu16.04 搭建及使用
  8. pytorch 0.4.0迁移指南
  9. AD域控服务器更改IP地址步骤
  10. PLSQL登录失败出现空白错误提示框的问题