模板引擎

指定模板: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     
 
posted on 2022-12-26 17:47  topass123  阅读(43)  评论(0编辑  收藏  举报