go html
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"}} //在当前模板中定义了另外一个模板,这里是模板命名
随机推荐
- JAVAWEB实现增删查改(图书信息管理)之修改功能实现
首先通过点击index.jsp页面的修改按钮,获取该行的id:↓ 其次,跳转到updateBooks.jsp页面进行修改信息,页面代码如下:↓ <%@ page import="Boo ...
- Java基础笔试练习(十二)
1.(C#.JAVA)扩展方法能访问被扩展对象的public成员 A.能 B.不能 答案: A 解析: 翻译一下,子类方法是否能够访问父类中的public成员. 2.如果子类要调用父类的构造函数,则通 ...
- Java开发笔记(一百二十九)Swing的输入框
Swing的输入框仍然分成两类:单行输入框和多行输入框,但与AWT的同类控件相比,它们在若干细节上有所调整.首先说单行输入框,AWT的单行输入框名叫TextField,平时输入什么字符它便显示什么字符 ...
- day28——C/S与B/S架构、网络通信原理、osi七层协议、UDP、TCP协议、TCP的三次握手与四次挥手
day28 C/S B/S架构 C:client 客户端 B:browse浏览器 S:server 服务端 C/S C/S架构:基于客户端与服务端之间的通信 QQ.游戏.皮皮虾 优点:个性化设 ...
- redis网文
1.为什么说Redis是单线程的以及Redis为什么这么快!https://blog.csdn.net/chenyao1994/article/details/794913372.Redis上踩过的一 ...
- shell中if条件字符串、数字比对,[[ ]]和[ ]区别
目录 shell 括号 test 和 []符号 [[]] 符号 let和(())符号 "[]" , "[[]]" 和 "(())"对比 sh ...
- 5. RDD编程进阶
5.1 累加器 累加器用来对信息进行聚合,通常在向Spark传递函数时,比如使用map()函数或者用filter()传条件时,可以使用驱动器程序中定义的变量,但是集群中运行的每个任务都会得到这些变量的 ...
- Ali-Tomcat 安装
通过在 Eclipse 安装 Tomcat4e 插件,或者在 Intellij Idea 安装配置 Ali-tomcat,可以快 速方便地启动并调试基于 EDAS 服务化框架 HSF 开发的应用. 1 ...
- jedis参数不当引发的问题总结
jedis参数不当引发dubbo服务线程池耗尽异常 现象:一个dubbo服务偶发性的出现个别机器甚至整个集群大量报线程池耗尽的问题.一开始对问题的处理比较粗暴,直接增加了10倍的线程数.但是问题依然偶 ...
- [洛谷P5367]【模板】康托展开
题目大意:给定一个$n$的排列,求它在$n$的全排列中的名次 题解:康托展开,对于一个全排列,第$i$为有$n+1-i$种选择,用变进制数表示,这一位就是$n+1-i$进制.记排列中第$[1,i)$中 ...