模板語法

#基本語法
go 統一使用了 {{ 和 }} 做爲左右標籤,沒有其餘的標籤符號。若是您想要修改成其它符號,能夠參考 模板標籤。html

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

模板中支持的 go 語言符號markdown

{{"string"}} // 通常 string
{{`raw string`}} // 原始 string
{{'c'}} // byte
{{print nil}} // nil 也被支持

#模板中的 pipelineide

能夠是上下文的變量輸出,也能夠是函數經過管道傳遞的返回值函數

{{. | FuncA | FuncB | FuncC}}

當 pipeline 的值等於:oop

false 或 0
nil 的指針或 interface
長度爲 0 的 array, slice, map, string
那麼這個 pipeline 被認爲是空this

#if … else … endurl

{{if pipeline}}{{end}}

if 判斷時,pipeline 爲空時,至關於判斷爲 False指針

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

支持嵌套的循環code

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

也可使用 else if 進行htm

{{if .IsHome}}
{{else if .IsAbout}}
{{else}}
{{end}}
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}}
with … end
{{with pipeline}}{{end}}

#with 用於重定向 pipeline

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

也能夠對變量賦值操

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

#wiith 也支持 else

{{with pipeline}}
{{else}}
    {{/* 當 pipeline 爲空時會執行這裏 */}}
{{end}}

#define
define 能夠用來定義自模板,可用於模塊定義和模板嵌套

{{define "loop"}}
    {{.Name}}
{{end}}

#使用 template 調用模板

{{range .Items}}
        {{template "loop" .}}
    {{end}}

template
{{template 「模板名」 pipeline}}
將對應的上下文 pipeline 傳給模板,才能夠在模板中調用

Beego 中支持直接載入文件模板

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

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

在模板中能夠接着載入其餘模板,對於模板的分模塊處理頗有用處

#註釋
容許多行文本註釋,不容許嵌套

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

基本函數
變量可使用符號 | 在函數間傳遞

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

使用括號

{{printf "nums is %s %d" (printf "%d %d" 1 2) 3}}
and
{{and .X .Y .Z}}

and 會逐一判斷每一個參數,將返回第一個爲空的參數,不然就返回最後一個非空參數

#call

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

call 能夠調用函數,並傳入參數

調用的函數須要返回 1 個值 或者 2 個值,返回兩個值時,第二個值用於返回 error 類型的錯誤。返回的錯誤不等於 nil 時,執行將終止。

#index
index 支持 map, slice, array, string,讀取指定類型對應下標的值

this.Data["Maps"] = map[string]string{"name": "Beego"}
{{index .Maps "name"}}
len
{{printf "The content length is %d" (.Content|len)}}

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

#not
not 返回輸入參數的否認值,if true then false else true

#or

{{or .X .Y .Z}}

or 會逐一判斷每一個參數,將返回第一個非空的參數,不然就返回最後一個參數

#print
對應 fmt.Sprint

#printf
對應 fmt.Sprintf

#println
對應 fmt.Sprintln

#urlquery
{{urlquery 「http://beego.me」}}
將返回

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

#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}}
相關文章
相關標籤/搜索