1. 程式人生 > >beego——模板語法

beego——模板語法

一、基本語法

go統一使用{{}}作為左右標籤,沒有其它的標籤符號。

使用"."來訪問當前位置的上下文,使用"$"來引用當前模板根級的上下文,使用$var來訪問建立的變數。

1.模板中支援的go語言符號

{{"string"}} // 一般 string
{{`raw string`}} // 原始 string
{{'c'}} // byte
{{print nil}} // nil 也被支援

  

2.模板中的pipeline(管道)

可以是上下文的變數輸出,也可以是函式通過管道傳遞的返回值。

{{. | FuncA | FuncB | FuncC}}

當pipeline的值等於:

  • false或0
  • nil的指標或interface
  • 長度為0的array、slice、map、string

那麼這個pipeline被認為是空。

 

3.邏輯處理

(1)if...else...end

{{if pipeline}}{{end}}

if判斷時,pipeline為空時,相當於判斷為False

this.Data["IsLogin"] = true
this.Data["IsHome"] = true
this.Data["IsAbout"] = true

支援巢狀的迴圈

{{if .IsHome}}
{{else}}
    {{if .IsAbout}}{{end}}
{{end}}

也可以使用else if進行

{{if .IsHome}}
{{else if .IsAbout}}
{{else}}
{{end}}

(2)range...end  內迴圈

{{range pipeline}}{{.}}{{end}}

pipeline 支援的型別為 array, slice, map, channel

range 迴圈內部的 . 改變為以上型別的子元素

對應的值長度為 0 時,range 不會執行,. 不會改變

pages := []struct {
    Num int
}{{10}, {20}, {30}}

this.Data["Total"] = 100
this.Data["Pages"] = pages

使用 .Num 輸出子元素的 Num 屬性,使用 $. 引用模板中的根級上下文

{{range .Pages}}
    {{.Num}} of {{$.Total}}
{{end}}

使用建立的變數,在這裡和 go 中的 range 用法是相同的。

{{range $index, $elem := .Pages}}
    {{$index}} - {{$elem.Num}} - {{.Num}} of {{$.Total}}
{{end}}

range 也支援 else

{{range .Pages}}
{{else}}
    {{/* 當 .Pages 為空 或者 長度為 0 時會執行這裡 */}}
{{end}}

(3)with...end

{{with pipeline}}{{end}}

with 用於重定向 pipeline

{{with .Field.NestField.SubField}}
    {{.Var}}
{{end}}

也可以對變數賦值操作

{{with $value := "My name is %s"}}
    {{printf . "slene"}}
{{end}}

with 也支援 else

{{with pipeline}}
{{else}}
    {{/* 當 pipeline 為空時會執行這裡 */}}
{{end}}

(4)define

define 可以用來定義自模板,可用於模組定義和模板巢狀

{{define "loop"}}
    <li>{{.Name}}</li>
{{end}}

使用 template 呼叫模板

<ul>
    {{range .Items}}
        {{template "loop" .}}
    {{end}}
</ul>

(5)template

{{template "模板名" pipeline}}

將對應的上下文 pipeline 傳給模板,才可以在模板中呼叫

 

3.beego中支援直接載入檔案模板

{{template "path/to/head.html" .}}

Beego 會依據你設定的模板路徑讀取 head.html

在模板中可以接著載入其他模板,對於模板的分模組處理很有用處

 

4.註釋

允許多行文字註釋,不允許巢狀

{{/* comment content
support new line */}}

  

 

二、基本函式

變數可以使用符號|在函式間傳遞

{{.Con | markdown | addlinks}}
{{.Name | printf "%s"}}  

使用括號

{{printf "nums is %s %d" (printf "%d %d" 1 2) 3}}

(1)and

{{and .X .Y .Z}}

and 會逐一判斷每個引數,將返回第一個為空的引數,否則就返回最後一個非空引數

(2)call

{{call .Field.Func .Arg1 .Arg2}}

call 可以呼叫函式,並傳入引數

呼叫的函式需要返回 1 個值 或者 2 個值,返回兩個值時,第二個值用於返回 error 型別的錯誤。返回的錯誤不等於 nil 時,執行將終止。

(3)index

index 支援 map, slice, array, string,讀取指定型別對應下標的值

this.Data["Maps"] = map[string]string{"name": "Beego"}
{{index .Maps "name"}}  

(4)len

{{printf "The content length is %d" (.Content|len)}}

返回對應型別的長度,支援型別:map, slice, array, string, chan

(5)not

not 返回輸入引數的否定值,if true then false else true

(6)or

{{or .X .Y .Z}}

or 會逐一判斷每個引數,將返回第一個非空的引數,否則就返回最後一個引數

(7)print

對應 fmt.Sprint

(8)printf

對應fmt.Sprintf

(9)pfintln

對應fmt.Sprintf

(10)urlquery

{{urlquery "http://beego.me"}}

將返回

http%3A%2F%2Fbeego.me

(11)eq / ne / lt / le / gt / ge

這類函式一般配合在 if 中使用

eq: arg1 == arg2
ne: arg1 != arg2
lt: arg1 < arg2
le: arg1 <= arg2
gt: arg1 > arg2
ge: arg1 >= arg2

eq 和其他函式不一樣的地方是,支援多個引數,和下面的邏輯判斷相同

arg1==arg2 || arg1==arg3 || arg1==arg4 ...

與 if 一起使用

{{if eq true .Var1 .Var2 .Var3}}{{end}}
{{if lt 100 200}}{{end}}