模板引擎
指定模板:c.TplName = "index.tpl"
默認支持tpl和html
beego.AddTemplateExt設置其他后綴
beego.AddTemplateExt("后綴名")
如果不設置該參數,那么默認會去到模板目錄的 Controller/<方法名>.tpl 查找,例如上面的方法會找 maincontroller/get.tpl
當然如果不想使用模板引擎的話,可以在配置文件設置
autorender = false
數據渲染
c.Data["Website"] = "beego.me"
前端獲取數據:{ {.Website } } 兩個換括號中變量名前有個點,這個點表示當前對象
結構體使用【.】
type student struct{
Name string
Age int
Gender string
}
?
賦值:
c.Data["student"] = &student{Name:"知了課堂",Age:18,Gender:"男"}
?
前端使用:
學生姓名:{{.student.Name}}
學生年齡:{{.student.Age}}
學生性別:{{.student.Gender}}
?
注意:結構體中的字段要在其他地方使用,比如首字母大寫
切片結構體【$】
結構體:
type student struct {
Name string
Age int
Gender string
}
?
賦值:
mapa := make(map[int]student)
mapa[101] = student{Name:"張三1",Age:181,Gender:"男"}
mapa[102] = student{Name:"張三2",Age:182,Gender:"男"}
mapa[103] = student{Name:"張三3",Age:183,Gender:"男"}
c.Data["hero_map"] = mapa
前端獲取:先循環數組,在獲取結構體變量,注意是大寫
{{range $v :=.hero_map}}
{{$v.Name}}
{{end}}
獲取傳遞的參數
url參數
id := c.Input().Get("id")
id2 := c.GetString("id")
獲取body的數據
s.Ctx.Input.RequestBody
本文來自博客園,作者:topass123,轉載請注明原文鏈接:http://www.rzrgm.cn/topass123/p/17006346.html
浙公網安備 33010602011771號