beego——view 模板語法


一、基本語法

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}}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM