---
title: "模板引擎-模板布局"
url: "https://goframe.org/docs/core/gview-layout"
markdown_url: "https://goframe.org/docs/core/gview-layout.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "核心组件"
category: "核心组件 / 模板引擎"
published: "2024-10-21"
updated: "2026-02-11"
description: "全面介绍`GoFrame`框架`gview`模板引擎的两种布局方式。第一种是使用`define`和`template`标签组合，通过`define`定义模板内容块、`template`标签跨文件引入内容块实现模块化布局。第二种是使用`include`标签直接嵌入模板文件实现页面组合。两种方式均支持模板变量传递，需要使用`.`符号传递上下文。通过完整的代码示例演示了如何构建包含`header`、`container`、`footer`的页面布局，以及如何实现动态内容切换的布局系统。"
keywords: ["GoFrame", "GoFrame框架", "gview", "模板引擎", "layout", "define", "template", "include", "模板布局", "模板变量"]
---

# 模板引擎-模板布局

`gview`模板引擎支持两种`layout`模板布局方式：

1. `define` + `template`方式
2. `include`模板嵌入方式

这两种方式均支持对模板变量的传入。

### `define` + `template`

由于`gview`底层采用了`ParseFiles`方式批量解析模板文件，因此可以使用`define`标签定义模板内容块，通过`template`标签在其他任意的模板文件中引入指定的模板内容块。`template`标签支持跨模板引用，也就是说`define`标签定义的模板内容块可能是在其他模板文件中，`template`也可以随意引入。
:::warning
注意：
- 为嵌套的子模板传递模板变量时，应当使用： `{{template "xxx" .}}` 的语法。
- 模板文件的后缀名，要跟 `define template` 文件的后缀名保持一致。
:::
使用示例：

![](https://goframe.org/markdown/5c50dcf4b78634b414c3857035097292.png)

1. `layout.html`
```html
<!DOCTYPE html>
<html>
<head>
    <title>GoFrame Layout</title>
    {{template "header" .}}
</head>
<body>
    <div class="container">
    {{template "container" .}}
    </div>
    <div class="footer">
    {{template "footer" .}}
    </div>
</body>
</html>
```

2. `header.html`
```html
{{define "header"}}
    <h1>{{.header}}</h1>
{{end}}
```

1. `container.html`
```html
{{define "container"}}
<h1>{{.container}}</h1>
{{end}}
```

1. `footer.html`
```html
{{define "footer"}}
<h1>{{.footer}}</h1>
{{end}}
```

1. `main.go`
```go
package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func main() {
    s := g.Server()
    s.BindHandler("/", func(r *ghttp.Request) {
        r.Response.WriteTpl("layout.html", g.Map{
            "header":    "This is header",
            "container": "This is container",
            "footer":    "This is footer",
        })
    })
    s.SetPort(8199)
    s.Run()
}
```

执行后，访问 [http://127.0.0.1:8199](http://127.0.0.1:8199/) 结果如下：

![](https://goframe.org/markdown/8826a2512ed91b4c9e7b77eabad4ae2c.png)

### `include`模板嵌入

当然我们也可以使用`include`标签来实现页面布局。
:::warning
注意，为嵌套的子模板传递模板变量时，应当使用： `{{include "xxx" .}}` 的语法。
:::
使用示例：

![](https://goframe.org/markdown/21b9ad277927db37879d5513766557c2.png)

1. `layout.html`
```html
{{include "header.html" .}}
{{include .mainTpl .}}
{{include "footer.html" .}}
```

2. `header.html`
```html
<h1>HEADER</h1>
```

3. `footer.html`
```html
<h1>FOOTER</h1>
```

4. `main1.html`
```html
<h1>MAIN1</h1>
```

1. `main2.html`
```html
<h1>MAIN2</h1>
```

1. `main.go`
```go
package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func main() {
    s := g.Server()
    s.BindHandler("/main1", func(r *ghttp.Request) {
        r.Response.WriteTpl("layout.html", g.Map{
            "mainTpl": "main/main1.html",
        })
    })
    s.BindHandler("/main2", func(r *ghttp.Request) {
        r.Response.WriteTpl("layout.html", g.Map{
            "mainTpl": "main/main2.html",
        })
    })
    s.SetPort(8199)
    s.Run()
}
```

执行后，访问不同的路由地址，将会看到不同的结果：

1. [http://127.0.0.1:8199/main1](http://127.0.0.1:8199/main1)

![](https://goframe.org/markdown/88d87dc79a4aa226d1c20312c3aeaff0.png)

2. [http://127.0.0.1:8199/main2](http://127.0.0.1:8199/main2)

![](https://goframe.org/markdown/8db789d456e0422ca3796242b89b8b44.png)
