astaxie/beego — pull request #409 by RickyLin (2013-12-23 16:50:51+00:00)
Title: Added LayoutSections property to Controller

users can define 0 or multipe sections in the Layout template file so
all kinds of content such as scripts, css can go to proper sections in
the generated html file.
我是一个 go 语言初学者，所以代码实现有所欠缺的地方，敬请指正。
这个 pull request 的原因是这样的。目前，一个 Layout 页只能存在一个 LayoutContent，内容页面的所有内容都包含在 LayoutContent 中。而对于一个复杂的 LayoutContent，其中可能包括有javascript脚本、CSS 引用等，根据惯例，通常 css 会放到 Head 元素中，javascript 脚本需要放到 body 元素的末尾，而其它内容则根据需要放在合适的位置。在 Layout 页中仅有一个 LayoutContent 是不够的。所以我在 Controller 中增加了一个 LayoutSections属性，可以允许 Layout 页中设置多个 section，然后每个 section 可以分别包含各自的子模板页。
For example, I have layout_blog.tpl as layout file. In blogs folder, I have index.tpl, html_head.tpl and scripts.tpl.
![screen shot 2013-12-24 at 12 30 44 am](https://f.cloud.github.com/assets/1393989/1801790/a509bb82-6bef-11e3-82e1-6b7d7a7a30c6.png)
The layout_blog.tpl:
``` html

Lin Li

{{.HtmlHead}}

{{.LayoutContent}}

{{.SideBar}}

{{.Scripts}}

```
the html_head.tpl:
``` html

```
the scripts.tpl:
``` html

```
in my blogs controller:
``` go
type BlogsController struct {
beego.Controller
}

func (this *BlogsController) Get() {
this.Layout = "layout_blog.tpl"
this.TplNames = "blogs/index.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["HtmlHead"] = "blogs/html_head.tpl"
this.LayoutSections["Scripts"] = "blogs/scripts.tpl"
this.LayoutSections["Sidebar"] = ""
}
```
I think we will get more flexibility on creating Layout by this way.