Goweb- action

Time:2020-2-16

Go template engine

action

The action of go template is to embed some commands into the template. These commands need to be put into two templates
In curly braces {{{action}}, we have used a very important action before: point (.), which represents passing to module
Data of the board. Now we will introduce some common actions. If you want to know more about other types of actions, please refer to
Document for the text / template library.

Conditional action

Format 1:

{{if Arg}} content to display {end}}

Format two:

{{if Arg}} content to display {{else}} content to display when the if condition is not met {end}}}

Where arg is the parameter passed to the conditional action, which can be a string constant
A variable, a function obtaining method that returns a single value, etc.
for example
template file

template file
    
  
  
    
    You are an adult! {{else}} you are not an adult {{end}}

Processor side code

func handler(w http.ResponseWriter, r *http.Request) {
//Parsing template files
t := template.Must(template.ParseFiles("hello.html"))
//Declare a variable
age := 16
//Execution template
t.Execute(w, age > 18)
}

Results in browser
You're underage

Iterative action

Iterative actions can iterate over arrays, slices, maps, or channels.

Format 1:

The element that {range.}} traverses is {.} {end}}}

Format two:

The element that {range.}} traverses is {.} {else}} without any element {end}}}

The point after the range represents the element to be traversed; the point in the content to be displayed represents the traversal
Elements arrived

For example:

template file

template file
    
  
  
    
    {{range .}}
    {{.}}
    {{else}} doesn't traverse anything {end}}

Processor side code

func handler(w http.ResponseWriter, r *http.Request) {
//Parsing template files
t := template.Must(template.ParseFiles("hello.html"))
//Declare a string slice
Stars: = [] string {"Ma Rong", "Li Xiaolu", "Bai Baihe"}
//Execution template
t.Execute(w, stars)
}

Results in browser

Ma Rong, Li Xiaolu, Bai Baihe

If the structure is one by one after iteration, the field name is used to get the field value in the structure
Mode acquisition

{{range.}} gets the name field name of the structure {. Name}} {{end}} iteration map
The variable can be set, starting with $: {range $k, $V: =.}} key is {$k}}}, value is{{
$V} {end}} iteration pipeline {C1 | C2 | C3}}}

C1, C2, and C3 can be parameters or functions. Pipes allow the user to output a parameter
Pass to the next parameter, and use | division between the parameters.

Set action

The set action allows you to set a value for a point (.) within a specified range.
Format 1:

The new value set by {with Arg} for the passed data is {.} {end}}

Format two:

The new value set by {with Arg}} for the passed data is {.}} {else}} the passed data is still{{
. }} {{ end }}

For example:

template file

template file
    
  
  
    
    The data obtained is: {{.}}
    {{with "Prince"}}
    The replaced data is: {{.}}
    {{end}}
    
    {{with ""}}
    Take a look at the data now: {{.}}
    {{else}}
    The data has not been replaced, or: {.}}
    {{end}}

Processor side code

func handler(w http.ResponseWriter, r *http.Request) {
//Parsing template files
t := template.Must(template.ParseFiles("hello.html"))
//Execution template
t. Execute (W, "civet")
}

Results in browser

The data is: civet
The replaced data is: Prince
The data has not been replaced, or: civet

Include action

Include action allows users to include another template in one template to build a nested template
Board.

Format 1:{{ template “name” }}

Name is the name of the included template

Format two:{{ template “name” arg }}

Arg is the data that the user wants to pass to the nested template

For example:

template file

hello.html

template file
    
  
  
    
    The data from the background is: {{.}}
    
    {{ template "hello2.html"}}
    End of hello.html file content
    
    将 hello.html template file中的数据传递给 hello2.html template file
    {{ template "hello2.html" . }}

hello2.html

Hello template file
    
  
  
    
    The data in the Hello 2.html template file is: {.}}

Processor side code

func handler(w http.ResponseWriter, r *http.Request) {
//Parsing template files
t := template.Must(template.ParseFiles("hello.html", "hello2.html"))
//Execution template
t. Execute (W, "test contains")
}

Note: when parsing the template file, both the current file and the included file should be parsed
Results in browser

The data from the background is: the test contains
The data in the Hello 2.html template file is:
End of hello.html file content
Transfer the data in the hello.html template file to the hello.html template file
The data in the Hello 2.html template file is:测试包含

Define actions

When we visit some websites, we often see the same parts in many pages: navigation bar, copyright
Information, contact information, etc. These same layouts can be implemented by defining actions to define templates in the template file
Now. The format of defining a template is: {define “layout”}} at the beginning and {end}} at the end.

  1. Define a template in a template file (Hello. HTML)
{{ define "model"}}

  
    template file
    
  
  
    {{ template "content"}}
  

{{ end }}
  1. Define multiple templates in one template file
    Template file (Hello. HTML)
{{ define "model"}}

  
    template file
    
  
  
    {{ template "content"}}
  

{{ end }} {{ define "content"}}
I have a surprise
{{ end }}

Processor side code

func handler(w http.ResponseWriter, r *http.Request) {
//Parsing template files
t := template.Must(template.ParseFiles("hello.html"))

//Execution template
t.ExecuteTemplate(w, "model", "")
}

Note: you need to call the executetemplate method and specify the name of the template

Results in browser

I have a surprise

  1. Define a template with the same name in a different template file
    template file
    hello.html
{{ define "model"}}

  
    template file
    
  
  
    {{ template "content"}}
  

{{ end }}

content1.html

Content template file
    
  
  
    
    {{ define “content” }}
    I am the content in the content1.html template file
    {{ end }}

content2.html

Content template file
    
  
  
    
    {{ define “content” }}
    I am the content in the content2.html template file
    {{ end }}

Processor side code

func handler(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().Unix())
var t *template.Template
if rand.Intn(5) > 2 {
//Parsing template files
t = template.Must(template.ParseFiles("hello.html",
"content1.html"))

} else {
//Parsing template files
t = template.Must(template.ParseFiles("hello.html",
"content2.html"))
}
//Execution template
t.ExecuteTemplate(w, "model", "")
}

Results in browser

I am the content in the content1.html template file

Block action

Go 1.6 introduces a new block action that allows users to define a template and use it immediately. Quite
Set a default template on

Format:

{{block Arg}} if I can't find the template, I will display {end}}

Modify the template file hello.html in 6.4.5

{{ define "model"}}

  
    template file
    
  

  
    {{block "content"}}} if it can't be found, it will show me {{end}}}
  

{{ end }}

Modify the code on the processor side a little

func handler(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().Unix())
var t *template.Template
if rand.Intn(5) > 2 {
//Parsing template files
t = template.Must(template.ParseFiles("hello.html",
"content1.html"))
} else {
//Parsing template files
t = template.Must(template.ParseFiles("hello.html"))
}
//Execution template
t.ExecuteTemplate(w, "model", "")
}

Results in browser

Show me if I can’t find it

Recommended Today

[Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat: 127.0.0.1:6379> keys * 1) “kkk” 2) “key1” 127.0.0.1:6379> randomkey “key1” 127.0.0.1:6379> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]