<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                快速開始 ~~~ func handler(w http.ResponseWriter,r *http.Request){ t := template.New("new template") //創建一個模板 t,err := t.ParseFile("tpl/welcome.html",nil) //解析模板文件 if err != nil{ panic(err.Error()) } user := GetUser() //獲取用戶信息 t.Execute(w,user) //執行模板,并通過w輸出 } ~~~ 各種模板 {{.}} 表示當前對象,如user對象 {{.FieldName}} 表示對象的某個字段 {{range …}}{{end}} go中for…range語法類似,循環 {{with …}}{{end}} 當前對象的值,上下文 ~~~ //一個模板文件例子 hello {{.UserName}}! {{range .Emails}} an email {{.}} {{end}} {{with .Friends}} {{range .}} my friend name is {{.Fname}} {{end}} {{end}} ~~~ {{if …}}{{else}}{{end}} go中的if-else語法類似,條件選擇 ~~~ //if 后只能出現bool值,不支持bool表達式 if-else demo: {{if .ShowFlag}} section if {{else}} section else. {{end}} ~~~ pipeline 左邊的輸出作為右邊的輸入 ~~~ //當前對象作為參數,傳入內置函數html中 {{. | html}} ~~~ 模板變量 模板使用過程中定義局部變量 ~~~ {{with $x := "output"}}{{$x}}{{end}} {{with $x := "output"}}{{$x | printf "%s"}}{{end}} ~~~ 輸出 ~~~ output output ~~~ 模板函數 支持的函數類型 ~~~ func(args ...interface{})string ~~~ 函數定義例子 ~~~ func Say(args ...interface{})string{ return fmt.Sprintf("%s %v","Hello",args[0]) } ~~~ 注冊函數 ~~~ t = t.Funcs(template.FuncMap{"Say":Say}) ~~~ 模板定義 ~~~ {{Say `Func`}} {{`Pipe` | Say}} ~~~ 輸出: ~~~ Hello Func Hello Pipe ~~~ template中內置函數 ~~~ var builtins = FuncMap{ "and": and, "call": call, "html": HTMLEscaper, "index": index, "js": JSEscaper, "len": length, "not": not, "or": or, "print": fmt.Sprint, "printf": fmt.Sprintf, "println": fmt.Sprintln, "urlquery": URLQueryEscaper, } ~~~ Must操作 檢測模板是否正確:大括號是否匹配,注釋是否正確關閉,變量是否正確 ~~~ tOk := template.New("first") template.Must(tOk.Parse(" some static text /* and a comment */")) fmt.Println("The first one parsed OK.") template.Must(template.New("second").Parse("some static text {{ .Name }}")) fmt.Println("The second one parsed OK.") fmt.Println("The next one ought to fail.") tErr := template.New("check parse error with Must") template.Must(tErr.Parse(" some static text {{ .Name }")) ~~~ 輸出: ~~~ The first one parsed OK. The second one parsed OK. The next one ought to fail. panic: template: check parse error with Must:1: unexpected "}" in command ~~~ 模板嵌套 將模板模塊化,如在web開發中定義header,content,footer三個子模塊 ~~~ //聲明 {{define "module_name"}}content{{end}} //調用 {{template "module_name"}} ~~~ 以下定義三個文件: - header.html ~~~ {{define "header"}} <html> <head> <title>演示信息</title> </head> <body> {{end}} ~~~ footer.tmpl ~~~ {{define "footer"}} </body> </html> {{end}} ~~~ content.tmpl ~~~ {{define "content"}} {{template "header"}} <h1>演示嵌套</h1> <ul> <li>嵌套使用define定義子模板</li> <li>調用使用template</li> </ul> {{template "footer"}} {{end}} ~~~ go代碼 ParseFiles:所有嵌套的模板全部解析到模板中 ~~~ s1,_ := template.ParseFiles("header.tmpl","content.tmpl","footer.tmpl") s1.ExecuteTemplate(w, "header",nil) s1.ExecuteTemplate(w, "footer", nil) s1.ExecuteTemplate(w, "content", nil) ~~~ ![](https://box.kancloud.cn/032176b8778dbc41f1ce6b79a83d59c1_900x350.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看