Go语言模板语法
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 string Gender string Age 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 main import ( "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) } }
-
-
这里包含了 对自定义函数,嵌套模板,以及传值的使用
-