摘要:本节主要介绍beego的基本模板语法、常用模板语法、常用模板函数、内置模板函数、自定义模板函数

一、调整目录结构

二、修改router.go\chapter01.go文件

//router.go
import "beegoProject/controllers/chapter01"

beego.Router("/chapter01", &chapter01.Chapter01Controller{})

//chapter01.go
package chapter01

import "github.com/axastaxie/beego"

type chapter01Controller struct{
    beego.Controller
}

func (c *chapter01Controller) Get(){
    array := []int{2,4,5,6,3}
    array1 := [3]int{}
    c.Data["name"] = "nina"
    c.Data["arr"] = array
    c.Data["arr1"] = array1
    c.TplName("chapter01/chapter01.html")
}

三、模板常用语法

1、基本语法

  • 使用.来访问当前位置的上下文
  • 使用$来引用当前模板根级的上下文
  • 使用 $. 引用模板中的根级上下文
.name:{{.name}} <br>

$v:
{{range $i,$v := .arr}}
    {{$v}},
{{end}}<br>

$.name:
{{range $i,$v := .arr}}
    {{$i}}
    {{$.name}}
{{end}}<br>

2、常用语法

  • 字符串:{{"nina"}}
  • 原始字符串:{{<tb></tb>}}
  • 字节类型返回 ascll 码:{{' a'}}
  • nil:{{print nil}}
① 字符串:{ {"nina"} }:{{"nina"}} <br>
② 原始字符串:{ { ` < tb > < /tb > ` } }:{{`<tb></tb>`}}不会转义 <br>
③ 字节类型返回 ascll 码:{ { ' a' } } --> {{'a'}} <br>
④ nil:{ { print nil } } :{{print nil}} <br>

3、range...else

{{range $i,$v := .arr1}}
    {{.}}
    {{else}}
    暂无数据
{{end}}

4、if...else

  • eq ==
  • ne !=
  • lt <
  • le <=
  • gt >
  • ge >=
{{if gt 16 20}}
    16大于20
    {{else}}
    16小于20
{{end}}

5、template

新建一个html文件

// header.html
我是header.html <br>
用户名:{{.name}}

//chapter01.html
{{template "/chapter01/header.html" .}}

四、模板常用函数

    // chapter02.go
    o := 3
    x := ""
    y := 1
    z := "z"
    h.Data["o"] = o
    h.Data["x"] = x
    h.Data["y"] = y
    h.Data["z"] = z
    h.TplName = "chapter02/chapter02.html"

1、and

只要有一个为空,则整体为空,如果都不为空,则返回最后一个

//chapter02.html
x,y,z 只要有一个为空,则整体为空: <br>
{{and .x .y .z}} <br>
o,y,z 如果都不为空,则返回最后一个: <br>
{{and .o .y .z}} <br>

2、or

只要有一个不为空,则返回第一个不为空的,否则返回空

//chapter02.html
o,x,y 只要有一个不为空,则返回第一个不为空的: <br>
{{or .o .x .y}}<br>

3、index

读取指定类型对应下标的值

// chapter02.go
h.Data["array1"] = []int{2,6,3,7,4,7}
h.Data["array2"] = map[string]interface{}{"name":"nina","age":21}
//chapter02.html
{{index .array1 3}}<br>
{{index .array2 "name"}}<br>

4、len

返回对应类型的长度

//chapter02.html
第一种:{{len . array1}}<br>
第二种:{{.array1 | len}}<br>

5、not

返回输入参数的否定值

//chapter.go
h.Data["is_ok"] = true
//chapter02.html
{{not .is_ok }}<br>

6、【内置函数】date

时间格式化函数

时间:2022-01-04 18:13:11.8182765 +0800 CST m=+1316.125867801

格式化后:2022-01-04 18:13:11

//chapter.go
h.Data["t"] = time.Now()
//chapter.html
{{date .t "Y-m-d H:i:s"}} <br>

7、【内置函数】compare

比较函数,返回true/false

//chapter.html
{{compare 18 19}} <br>
{{compare 'a' 97}}<br>

8、【内置函数】substr

截取字符串

//chapter.html
{{substr "啊手动阀手动阀各个地方癸未日" 0 5}}

9、【内置函数】map_get

获取map的指定key值

//chapter02.html
{{map_get .array2 "name"}}

10、【内置函数】urlfor

//chapter02.html
{{urlfor "Chapter02Controller.Get"}}

五、自定义模板函数

自定义一个 subStrAndRep 函数,使其能够截取字符串后加上 “...”

注意:AddFuncMap要在Run前面

//chapter02.go
func subStrAndRep(str string, start,end int) string{
    if len(str) > end{
        return str[start:end] + "..."
    } else{
        return "错误,超出字符串长度"
    }
}

//main.go
func main(){
    beego.AddFuncMap("substr_rep",chapter02.subStrAndRep)
    beego.Run()
}
{{substr_rep "ghjfytuioio" 0 5}}