為Go Web App 建立一個首頁面

來源:互聯網
上載者:User

標籤:

原文地址

?? 大多數web app都有一個相同的布局。這個布局可能包含一個header或者footer,甚至可能包含一個導覽功能表。Go的標準庫提供一個簡單的方式來建立這些基本元素,通過被不同的頁面重用,建立出模板頁的效果。
?? 這個簡單的例子來解釋如何?的:
?? 讓我們來建立一個簡單的包含兩個view的web app,一個是 main 一個是about。這兩個view都有相同的header和footer。
?? header模板的代碼如下:

{{ define "header" }}<!DOCTYPE html><html>    <head>        <title>{{.Title}}</title>        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css">        <style type="text/css">            body {padding-bottom: 70px;}            .content {margin:10px;}        </style>    </head>    <body>        <nav class="navbar navbar-default" role="navigation">          <div class="navbar-header">            <a class="navbar-brand" href="/">Go App</a>          </div>          <div class="collapse navbar-collapse navbar-ex1-collapse">              <ul class="nav navbar-nav">                <li><a href="/">Main</a></li>                <li><a href="/about">About</a></li>            </ul>          </div>        </nav>{{ end }}

footer模板的代碼如下:

{{ define "footer" }}        <p class="navbar-text navbar-fixed-bottom">Go Rocks!</p>            <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>    </body></html>{{ end }}
main 模板的代碼如下:
{{define "main"}}{{template "header" .}}<div class="content">    <h2>Main</h2>    <div>This is the Main page</div></div>{{template "footer" .}}{{end}}
? about 模板的代碼如下:
{{define "about"}}{{template "header" .}}<div class="content">    <h2>About</h2>    <div>This is the About page</div></div>{{template "footer" .}}{{end}}
伺服器代碼如下:
package mainimport (    "html/template"    "net/http")//Compile templates on startvar templates = template.Must(template.ParseFiles("header.html", "footer.html", "main.html", "about.html"))//A Page structuretype Page struct {    Title string}//Display the named templatefunc display(w http.ResponseWriter, tmpl string, data interface{}) {    templates.ExecuteTemplate(w, tmpl, data)}//The handlers.func mainHandler(w http.ResponseWriter, r *http.Request) {    display(w, "main", &Page{Title: "Home"})}func aboutHandler(w http.ResponseWriter, r *http.Request) {    display(w, "about", &Page{Title: "About"})}func main() {    http.HandleFunc("/", mainHandler)    http.HandleFunc("/about", aboutHandler)    //Listen on port 8080    http.ListenAndServe(":8080", nil)}

每一個模板頁都有一個define "name" 的命令來定義模板的名字。main和about頁面通過template "name"來包含header和footer。”.” 出入上下文來命名模板。現在,不管main和about頁面如何執行,他們的頁面都會包含header和footer。
?? 兩個頁面的結果如下:

為Go Web App 建立一個首頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.