1. 程式人生 > >Beego框架學習---layout的使用

Beego框架學習---layout的使用

css route mark tom 界面 span 組件 page mes

Beego框架學習---layout的使用

在管理系統中,管理菜單的界面是固定的,會變化的只是中間的部分。我就在想,是不是跟angular的“組件模塊的router-outlet一樣”每次只用修改中間的內容部分,菜單和導航欄不變。

果不其然,beego框架也是支持layout的設計。

  • 首先,在views目錄下創建一個layout.html,這個html文件中把整個的頁面框架設計好,在需要替換的內容的地方寫成{{.layoutContent}}

       <div>
    <!-- Content Header (Page header) -->
    {{.ContentHead}}
    <!-- Main content -->
    {{.LayoutContent }}
       </div>
  • 然後再路由轉到的controller的方法內,定義頁面的布局和內容。

      func (this *Usercontroller)Get()  {
    this.Data["Title"] = "用戶管理"
    this.Layout = "layout.html"
    this.LayoutSections = make(map[string]string)
    this.LayoutSections["CustomJs"] = "customjs/user.html"
    this.LayoutSections["CustomCss"] = ""
    this.LayoutSections["ContentHead"
    ] = "contenthead2.html" this.LayoutSections["Menu"] = "menu.html" this.TplName = "user.html" this.Data["undesgin"], _ = mgo.Listuser()
  • 此時,beego就會首先解析TplNames指定的文件,獲取內容賦值給LayoutContent,然後最後渲染layout.html文件。展示user.html頁面

Beego框架學習---layout的使用