如何做网站拥有自己的地址免费建站平台官网
 Go语言模板语法  
 
 
文章目录
- <center> Go语言模板语法
 - 连接前后端的符号: `{{}}`
 - 注释
 - 管道(pipeline)
 - 变量
 - 条件判断
 - range 关键字
 - with 关键字
 - 比较函数
 - 自定义函数
 - 嵌套模板
 - 模板继承
 
连接前后端的符号: {{}}
 
-  
模板语法都包含在
{{}}之中,其中{{.}}中的.表示当前对象.在传入一个结构体对象时,可以根据"."来访问结构体的对应字段.如果是复合类型数据,则可以通过{{.FiledName}}来访问它的字段,FiledName是指对应go代码中的结构体变量名 -  
伪代码例子:
//在html文档中 <body><p>Hello {{.Name}}</p><p>Gender {{.Gemder}}</p> <p>Age {{.Age}}</p> </body>//在Go代码中 type UserInfo struct{Name stringGender stringAge int } user := &UserInfo{Name:"李四",Gender: "未知",Age: 24, } -  
这样后端数据就传输到前端显示了
 
注释
- 语法: 
{{/*注释内容*/}} - 作用: 和其他语言的注释一致,目的是提升代码的可阅读性
 - 注意: 可以进行多行注释,但是不可以嵌套注释,并且必须紧贴分界符始止
 
管道(pipeline)
- 介绍: 管道是指产生数据的操作,比如"{{.}}" , "{{.Name}}“等,Go语言模板语法中支持使用管道符号”|"链接多个指令,用法和UNIX下的管道类似,所以说管道就像一种概念,知道就行了
 
变量
- 介绍: 在Action里可以初始化一个变量来捕获管道的执行结果 
- Action: 在Web开发中,Action通常指代处理HTTP请求的处理程序或函数。当用户在网页上执行某个操作(例如点击按钮、提交表单)时,相应的Action会被调用来处理请求并执行相应的逻辑操作。
 
 - 语法:
$variable := pipeline - 特点: 声明变量的Action不会产生任何输出,大概意思是仅仅声明该变量而没有对其进行赋值或使用时,程序的输出不会显示该变量的值或其他信息。只有在对变量进行赋值并在程序中使用时,才能在输出中看到相关的内容。
 
条件判断
- 语法: 
{{if pipeline}} T1 {{end}}{{if pipeline}} T1 {{else}} T0 {{end}}{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
 
range 关键字
- 在Go中使用range关键字,pipeline的值必须是数组,切片,字典或者通道.
 - 语法: 
{{range pipeline}} T1 {{end}}{{range pipeline}} T1 {{else}} T0 {{end}}
 
with 关键字
- with关键字和if关键字有点类似,"{{with}}"操作仅在传递的管道不为空时有条件地执行其主体
 - 语法: 
{{with pipeline}} T1 {{end}}{{with pipeline}} T1 {{else}} T0 {{end}}
 
比较函数
| 符号 | 作用 | 
|---|---|
eq | == | 
ne | != | 
lt | < | 
le | <= | 
gt | > | 
ge | >= | 
- 特点: 只有
eq符号可以接收多个参数,它会将第一个参数和其余参数一次比较 - 例子: 
{{eq arg1 arg2 arg3}} ==> arg1== arg2||arg1 == arg3 
自定义函数
-  
在嵌套模板中存在预定义函数(官方已经定义好的)和自定义函数(自己定义的),自定义函数使得模板更加灵活.
 -  
自定义函数通过调用
Funcs()方法实现Funcs()方法定义:func (t *Template) Funcs(funcMap FuncMap) *TemplateFuncMapd定义:type FuncMap map[string]interface{}
 -  
定义步骤:
-  
1.先到后端代码中定义一个函数: (匿名函数为例)
-  
hello := func() (string){return "Hello!" } 
 -  
 -  
2.调用
Funcs()方法-  
注意: 调用
Funcs()方法,需要在解析模板前调用,也就是在Parse()方法前 -  
bytes,err := ioutil.ReadFile("文件名/文件路径") templl,err := template.New("为模板命名(尽量和文件名字一致)").Funcs(template.FuncMap{"hello":hello}).Parse(string(bytes)) 
 -  
 -  
- 在模板中调用函数:
 
{{hello}}- 这里的
hello是指定FuncMap中的key,也就是上面的"hello" 
 
 -  
 
嵌套模板
-  
介绍: 嵌套模板也就是,在一个html文件中使用多个模板,被嵌套的模板可以是一个单独的
html文件,还可以利用define关键字在该模板下定义一个模板define语法: {{define “name”}} T(内容) {{end}}
 -  
通过
template关键字来执行模板- 语法: 
{{template "name"}}{{template "name" pipeline}}
 
 - 语法: 
 -  
例子:(模板代码)
Demo.html-  
<html><body><h1>{{template "title.html"}}</h1><div>{{template "content.html"}}</div></body> </html> {{/*define 的定义写在html标签之下*/}} {{define "content.html"}}<li>小明</li><li>小红</li> {{end}} -  
title.html文件定义-  
<ul><il>你好</il> </ul> 
 -  
 
 -  
 -  
后端代码(主要):
-  
tmpl,err :=template.ParseFiles("Demo.html路径","title.html路径") -  
在解析代码的时候,需要将包含其他模板的文件写在第一位,其余的可随意顺序
 
 -  
 -  
相关类似代码:
-  
hello.html文件
-  
<p>大家好,我是小黑子</p> 
 -  
 -  
Sever.html文件
-  
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>嵌套模板</title> </head> <body>{{/*自定义函数加模板嵌套*/}}<h1>{{Hello}}</h1><hr><h2>{{template "hello.html"}}</h2><p>{{.}}真帅!</p><hr>{{template "come.html"}} </body> </html> {{define "come.html"}} <ul><li>唱</li><li>跳</li><li>rap</li><li>篮球</li> </ul> {{end}} 
 -  
 -  
后端代码:
-  
package mainimport ("html/template""log""net/http" )func main() {http.HandleFunc("/", hello)http.ListenAndServe(":9000", nil) }func hello(w http.ResponseWriter, r *http.Request) {//定义模板//解析模板,自定义函数Hello := func() string {return "--- 欢迎来到我的舞台 ---"}tmpl, err := template.New("Sever.html").Funcs(template.FuncMap{"Hello": Hello}).ParseFiles("src\\使用html和template包\\Go语言模板语法\\嵌套模板\\Sever.html","src\\使用html和template包\\Go语言模板语法\\嵌套模板\\hello.html")if err != nil {log.Println("解析模板失败!")return}name := "贤哥"//渲染模板err = tmpl.Execute(w, name)if err != nil {log.Println("渲染模板失败!:", err)} } 
 -  
 -  
这里包含了 对自定义函数,嵌套模板,以及传值的使用
 
 -  
 
模板继承
-  
介绍: 模板继承是指对各种类似的固定板块的复用,例如说很多时候我们的开发网站的时候,其实不同的网页很多的地方都是类似的,所以说利用模板继承可以复用模板,减少工作量,代码也更加简洁
 -  
关键字
block- 语法:
{{block "name" pipeline}} T {{end}} 
 - 语法:
 -  
需要继承的模板,需要先继承根模板,然后再利用define关键字定义内容
 -  
例子
-  
base.tmp文件<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>模板继承</title> </head> <style>*{background-color:white;} </style> <body> <h1 style="color:red">模板继承</h1> <hr> <h2 style="color:blue">变化的内容:</h2> <div>{{block "context" .}}<p>会发生变化的地方</p><div><a href="http://localhost:9000/index">Index页面</a><br><a href="http://localhost:9000/home">Home页面</a></div>{{end}}</div> </body> </html>- 其中涉及一点点的
css内容,不影响阅读 
 - 其中涉及一点点的
 -  
index.tmpl文件{{/*继承根模板*/}} {{template "base.tmpl" .}} {{/*定义块模板*/}} {{define "context"}} <h2>Index页面</h2> <p>Hello,{{.}}</p> <a href="http://localhost:9000">Base页面</a> {{end}}- 先是继承然后定义内容,其中
{{template "base.tmpl" .}}的" . “指传入的值,而<p>Hello,{{.}}</p>的” . "接收该值 - 还有这里define后面的文件名,一定要与block时定义的名字一致
 
 - 先是继承然后定义内容,其中
 -  
home.tmpl文件{{/*继承根模板*/}} {{template "base.tmpl" .}} {{/*定义块模板*/}} {{define "context"}}<h2>Home页面</h2><p>Hello,{{.}}</p><a href="http://localhost:9000">Base页面</a> {{end}}- 和上面的基本一致
 
 -  
后端代码
package mainimport ("html/template""log""net/http" )func main() {http.HandleFunc("/", base)http.HandleFunc("/index", index)http.HandleFunc("/home", home)err := http.ListenAndServe(":9000", nil)if err != nil {log.Println(err)return} }func base(w http.ResponseWriter, r *http.Request) {//定义模板//解析模板t, err := template.New("base.tmpl").ParseFiles("src\\使用html和template包\\Go语言模板语法\\模板继承\\base.tmpl")if err != nil {log.Println("解析失败:", err)return}//渲染模板err = t.Execute(w, nil)if err != nil {log.Println("渲染失败:", err)return} }func index(w http.ResponseWriter, r *http.Request) {//定义模板//解析模板(涉及嵌套模板)t, err := template.New("base.tmpl").ParseFiles("src\\使用html和template包\\Go语言模板语法\\模板继承\\base.tmpl","src\\使用html和template包\\Go语言模板语法\\模板继承\\index.tmpl")if err != nil {log.Println("解析失败:", err)return}name := "贤哥!"//渲染模板err = t.ExecuteTemplate(w, "index.tmpl", name)if err != nil {log.Println("渲染失败:", err)return} } func home(w http.ResponseWriter, r *http.Request) {//定义模板//解析模板t, err := template.New("base.tmpl").ParseFiles("src\\使用html和template包\\Go语言模板语法\\模板继承\\base.tmpl","src\\使用html和template包\\Go语言模板语法\\模板继承\\home.tmpl")if err != nil {log.Println("解析失败:", err)return}name := "振哥!"//渲染模板err = t.ExecuteTemplate(w, "home.tmpl", name)if err != nil {log.Println("渲染失败:", err)return} } 
 -  
 
