beego - view template syntax

First, the basic grammar

go unified using {{ and }} as a side tag, no other label symbols.

Use " . " To access the current position of the context, use the " $ " to refer to the context of the current template at the root level, use $ var to access variables created.

1. Template support go sign language

{{"string"}} // 一般 string
{{ `Raw string`}} // original string
{{‘c‘}} // byte
{{Print nil}} // nil also supported

  

2. templates pipeline (Pipeline)

Context variables may be output, the function may be passed through the return conduit value.

{{. | FuncA | FuncB | FuncC}}

When the pipeline is equal to the value of:

  • false or 0
  • nil pointer or interface
  • 0 length of array, slice, map, string

Then the pipeline is considered to be empty.

 

3. Processing logic

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

{{if pipeline}}{{end}}

When if is determined, Pipeline empty, it is determined that corresponds to False

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

Support for nested loops

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

You can also use else if carried out

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

(2) range ... inner end loop

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

pipeline support of type array, slice, map, channel

circulating inside the range  . is changed to a child element of the above type

Value corresponding to the length is 0, range is not performed, . will not change

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

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

Use  .Num Num attribute output sub-element , using the  $. reference template context root level

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

Use variables created here and go in the range of usage is the same.

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

range also supports else

{{range .Pages}}
{{else}}
    {{/ * .Pages is empty or when a length of 0 is executed here * /}}
{{end}}

(3)with...end

{{with pipeline}}{{end}}

with a redirection pipeline

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

It can also be variable assignment

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

with also supports else

{{with pipeline}}
{{else}}
    {{/ * When the pipeline is empty herein performs * /}}
{{end}}

(4)define

can be used to define the definition from the template can be used in the module definition and nesting the templates

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

Use template template calls

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

(5)template

{{Template "template name" pipeline}}

The corresponding template context pipeline to pass before they can call in the template

 

3.beego in support direct loading template file

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

Beego reads head.html based template path that you set

In the template may then load additional templates, a template scoring module for processing useful

 

4. Comment

It allows multiple lines of text notes, can not be nested

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

  

 

Second, the basic functions

Variables can use the symbol | passed between functions

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

Use parentheses

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

(1)and

{{and .X .Y .Z}}

and one by one judge each parameter will return the first argument is null, otherwise it returns the last non-null parameter

(2)call

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

call can call the function, passing parameters

Function call needs to return a value or two values, return two values, the second value is used to return error type of error. When an error is returned is not equal to nil, execution will be terminated.

(3)index

Support index map, slice, array, string, reads the specified target value corresponding to the type

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

(4) Only

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

Returns the type corresponding to the length, the type of support: map, slice, array, string, chan

(5)not

not return a negative value of the input parameters, if true then false else true

(6)or

{{or .X .Y .Z}}

or one by one judge each parameter will return the first non-empty argument, otherwise it returns the last argument

(7)print

Correspondence fmt.Sprint

(8)printf

Correspondence fmt.Sprintf

(9) pfintln

Correspondence fmt.Sprintf

(10)urlquery

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

Will return

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

(11) eq / ne / lt / a / g / t ge

Such functions are typically used in conjunction with the if

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

eq and other functions not the same place that supports a plurality of parameters, and following the same logic determines

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

If used in conjunction with

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

Guess you like

Origin www.cnblogs.com/show58/p/12353398.html
Recommended