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

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

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

果不其然,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頁面
相關文章
相關標籤/搜索