Go template syntax of the language

. 1  Package main
 2  
. 3  Import (
 . 4      " HTML / Template " 
. 5      " Math / RAND " 
. 6      " NET / HTTP " 
. 7      " Time " 
. 8  )
 . 9  
10 FUNC Process (http.ResponseWriter W, R & lt * http.Request) {
 . 11      T : = template.Must (template.ParseFiles ( " tmpl.html " )) // parse the file to generate a template to develop an object 
12      / * 
13          rand.Intn () function is a pseudo-random function, no matter how many runs will only return the same random number, because it is a single default resource value,
 14         We must call rand.Seed (), and a change in the value passed as a parameter, such as time.Now (). UnixNano (), is always changing values can be generated.
 15      * / 
16      rand.Seed (Time.now ( ) .Unix ())
 . 17      t.Execute (W, rand.Intn ( 10 )> . 5 ) // returns bool value
 18 is  
. 19      // t.Execute (W, "Hello World") // use to write data to render the W
 20 is      // If the template is a set of the following specified method is invoked to render the template
 21 is      // t.ExecuteTemplate (W, "tmpl.html", "HelloWorld") 
22 is  }
 23 is  
24  // iteration 
25 FUNC tmplRange (W http.ResponseWriter, R & lt * http.Request) {
 26 is     T: = template.Must (template.ParseFiles ( " tmplrange.html " )) // parse the object template file generating formulation
 27      // create a string array 
28      DayOfWeek: = [] String { " Mon " , " Tue " , " Wen " , " Thu " , " Fri " , " SAT " , " Sun " }
 29      // write result 
30      t.Execute (W,
dayofWeek) 31  }
32  
33 is  // function 
34 is FUNC FormatData (T time.time) String {
 35      layout: = " 2006-01-02 " 
36      return t.Format (layout)
 37 [  }
 38 is  
39 FUNC tmplfunc (http.ResponseWriter W, R & lt * HTTP .request) {
 40      // create a funcmap mapping function to map the fdata FormatData 
41 is      funcMap: = {template.FuncMap " fdata " :} FormatData
 42 is      // create a mapping function to the front template and the template 
43 is      T: = template.New ( " tmplfunc.html ") .Funcs (funcMap)
 44 is      // parse template 
45      T, _ = t.ParseFiles ( " tmplfunc.html " )
 46 is      // return result 
47      t.Execute (W, Time.now ())
 48  }
 49  
50  // nesting the templates 
51 is FUNC tmplNested (http.ResponseWriter W, R & lt * http.Request) {
 52 is      T, _: = template.ParseFiles ( " tmpllayout.html " , " tmplcontent.html " )
 53 is      t.Execute (W, "" )
 54 is  }
 55  
56 is  FUNC main () {
57     http.HandleFunc("/process", process)
58     http.HandleFunc("/range", tmplRange)
59     http.HandleFunc("/func", tmplfunc)
60     http.HandleFunc("/nested",tmplNested)
61     http.ListenAndServe(":8080", nil)
62 
63 }
// iteration 
. 1
<! DOCTYPE HTML> 2 <HTML lang = " EN " > . 3 <head> . 4 <Meta charset = " UTF-. 8 " > . 5 <title> the Title </ title> . 6 </ head> . 7 <body > . 8 <h1 of> ====================================== </ h1 of> . 9 {{ .}} Range 10 . <Li> {{}} </ Li> . 11 {{}} End 12 is <h1 of> parameter can be passed to a further intermediate parameters | spaced </ h1 of> 13 is {{ 12.3456 | printf " %.2f"}} 14 </body> 15 </html>
// template functions 

. 1
<! DOCTYPE HTML> 2 <HTML lang = " EN " > . 3 <head> . 4 <Meta charset = " UTF-. 8 " > . 5 <title> FUNC </ title> . 6 </ head> . 7 < body> . 8 the current time is: {{|. the FDATA}} . 9 </ body> 10 </ HTML>
//模板嵌套
1
<!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>Hello world!</h1> 9 </body> 10 </html>
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 {{ template  "tmplcontent.html"}}
 9 {{ template  "ol.html"}}
10 </body>
11 </html>
12 
13 {{ define "ol.html"}}
14     <OL>
 15          <Li> eat </ Li>
 16          <Li> sleep </ Li>
 . 17          <Li> play Peas </ Li>
 18 is      </ OL>
 . 19 {{}} End

 

Guess you like

Origin www.cnblogs.com/yh2924/p/12602907.html