Golang Gin 模板基本使用
一, 模板定義,模板變量,條件判斷,模板函數,模板命名
1 {{define "default/index.html"}} 2 <!DOCTYPE html> 3 <html lang="en"> 4 5 <head> 6 <meta charset="UTF-8"> 7 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 8 <title>Document</title> 9 <link rel="stylesheet" href="/static/css/base.css"> 10 </head> 11 12 <body> 13 <h2>{{.title}}</h2> 14 <br> 15 <!-- 定義變量 --> 16 {{$t := .title}} 17 <h4>{{$t}}</h4> 18 <br> 19 <pre> 20 eq 如果 arg1 == arg2 則返回真 21 ne 如果 arg1 != arg2 則返回真 22 lt 如果 arg1 < arg2 則返回真 23 le 如果 arg1 <=arg2 則返回真 24 gt 如果 arg1> arg2 則返回真 25 ge 如果 arg1 >= arg2 則返回真 26 </pre> 27 <br> 28 <h3>條件判斷</h3> 29 <pre> 30 {{if ge .score 85}} 31 <p>優秀</p> 32 {{else if ge .score 60}} 33 <p>及格</p> 34 {{else}} 35 <p>不及格</p> 36 {{end}} 37 </pre> 38 <br> 39 <h3>range 遍歷數據</h3> 40 <ul> 41 {{range $key,$value:=.hobby}} 42 <li>{{$key}}:{{$value}}</li> 43 {{end}} 44 </ul> 45 46 <ul> 47 {{range $key, $value := .testSlice}} 48 <li>{{$key}}:{{$value}}</li> 49 {{else}} 50 <li>數組中沒有數據</li> 51 {{end}} 52 53 </ul> 54 55 <ul> 56 {{range $key, $value := .news}} 57 <li>{{$key}}:{{$value.Title}}:{{$value.Content}}</li> 58 {{end}} 59 </ul> 60 <br> 61 <h3>with 解構結構體</h3> 62 <p>原來的寫法:{{.testWith.Title}}</p> 63 <p>with寫法: 64 65 {{with .testWith}} 66 {{.Title}} 67 {{.Content}} 68 {{end}} 69 </p> 70 <br> 71 <h3>預定義模板函數</h3> 72 <pre> 73 執行模板時,函數從兩個函數字典中查找,首先時模板函數字典,然后時全局函數字典. 74 一般不再模板內定義函數,而是使用Funcs方法添加函數到模板里. 75 and : 函數返回它的第一個empty參數或者最后一個參數. 76 就是說"and x y" 等價于 "if x then y else x" 所有參數都會執行 77 or : 返回第一個非empty參數或者最后一個參數. 78 亦及"or x y"等價于"if x then x else y" 所有參數都會執行 79 not : 返回它的單個參數的bool值的否定 80 len : 返回它的參數的整數類型長度 81 index: 執行結果為第一個參數以剩下的參數為所有/鍵指向的值 82 如"index x 1 2 3"返回 x[1][2][3]的值;每個被索引的主題必須時數組\切片或者字典. 83 print: 即 fmt.Sprint 84 printf: 即 fmt.Sprintf 85 println: 即 fmt.Sprintln 86 html: 返回與其參數的文本表示形式等效的轉義HTML 87 這個函數在html/template中不可用 88 urlquery:以適合嵌入到網址查詢中的形式返回其參數的文本表示的轉義值. 89 這個函數在html/template中不可用 90 js: 返回與其參數的文本表示形式等效的轉義JavaScript 91 call: 執行結果是調用第一個參數的返回值,該參數必須是函數類型,其余參數作為調用該函數的參數. 92 如"call .X.Y 1 2" 等價于"dot.X.Y(1,2)" 93 其中Y是函數類型的字段或者字典的值,或者其他類似情況 94 call 的第一個參數的執行結果必須是函數類型的值,(和定義的print明顯不同) 95 該函數類型必須有1到2個返回值,如果有兩個則后一個必須是error接口類型 96 如果有2個返回值的方法返回的error非nil,模板執行會中斷并返回給調用模板執行者該錯誤 97 98 </pre> 99 <br> 100 <h3>自定義模板函數</h3> 101 <pre> 102 {{.date}} 103 {{UnixToTime .date}} 104 {{Println .title .msg}} 105 </pre> 106 <br> 107 <h3>模板引用</h3> 108 <pre> 109 注意: 1. public/page_header.html 模板已經定義好了 110 2. 引入的時候注意最后的(.) 將當前頁面的數據變量傳進去 111 {{template "public/page_header.html" .}} 112 </pre> 113 114 <br> 115 116 </body> 117 118 </html> 119 {{end}}
二, 模板加載,自定義模板函數,配置靜態web目錄
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "text/template" 7 "time" 8 9 "github.com/gin-gonic/gin" 10 ) 11 12 func main() { 13 TestTemelates() 14 } 15 16 func UnixToTime(timestamp int) (result string, err error) { 17 fmt.Println(timestamp) 18 t := time.Unix(int64(timestamp), 0) 19 return t.Format("2006-01-02 15:04:05"), nil 20 21 } 22 23 func Println(str1, str2 string) string { 24 fmt.Println(str1, str2) 25 return str1 + str2 26 } 27 28 // 命名模板 29 func TestTemelates() { 30 router := gin.Default() 31 32 // 自定義模板函數. 必須在加載模板之前SetFuncMap 33 router.SetFuncMap( 34 template.FuncMap{ 35 "UnixToTime": UnixToTime, 36 "Println": Println, 37 }) 38 39 // templates/**/* 代表加載templates下的所有二級目錄下的所有模板 40 // templates/**/**/* 代表加載templates下的所有三級目錄下的所有模板 41 // 模板分組就是將二級或三級目錄的名字寫進去 42 router.LoadHTMLGlob("templates/**/*") 43 44 // 配置靜態web目錄 第一個參數表示路由 第二個參數表示映射的目錄 45 // 這樣你就可以通過: localhost:8080/static/css/base.css 46 // 頁面引入: <link rel="stylesheet" href="/static/css/base.css"> 47 router.Static("/static", "./static") 48 49 router.GET("/news", func(c *gin.Context) { 50 c.HTML(http.StatusOK, "default/news.html", gin.H{ 51 "title": "新聞頁面", 52 }) 53 }) 54 55 router.GET("/index", func(c *gin.Context) { 56 c.HTML(http.StatusOK, "default/index.html", gin.H{ 57 "title": "首頁", 58 "score": 85, 59 "hobby": []string{"吃飯", "睡覺", "寫代碼", "玩游戲"}, 60 "news": []interface{}{ 61 &Article{ 62 Title: "標題1", 63 Content: "詳情1", 64 }, 65 &Article{ 66 Title: "標題2", 67 Content: "詳情2", 68 }, 69 }, 70 "testSlice": []string{}, 71 "testWith": &Article{ 72 Title: "標題3", 73 Content: "詳情3", 74 }, 75 "date": 1700636995, 76 "msg": "===", 77 }) 78 }) 79 router.Run("0.0.0.0:8080") 80 }

浙公網安備 33010602011771號