The gview template engine supports two types of layout template layouts:
define+templatemethodincludetemplate embedding method
Both methods support passing template variables.
define + template
Since gview uses ParseFiles in the underlying layer to parse template files in bulk, you can use the define tag to define template content blocks and use the template tag to introduce specified template content blocks in any other template files. The template tag supports cross-template referencing, meaning that the template content block defined by the define tag may be in other template files, and the template can be freely introduced as well.
Note:
- When passing template variables to a nested child template, use the syntax:
{{template "xxx" .}}. - The file extension of the template file should be consistent with the
define templatefile extension.
Example Usage:

layout.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>
header.html
{{define "header"}}
<h1>{{.header}}</h1>
{{end}}
container.html
{{define "container"}}
<h1>{{.container}}</h1>
{{end}}
footer.html
{{define "footer"}}
<h1>{{.footer}}</h1>
{{end}}
main.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()
}
After execution, visit http://127.0.0.1:8199 and the result is as follows:

include Template Embedding
Of course, we can also use the include tag to achieve page layout.
Note: When passing template variables to a nested child template, use the syntax: {{include "xxx" .}}.
Example Usage:

layout.html
{{include "header.html" .}}
{{include .mainTpl .}}
{{include "footer.html" .}}
header.html
<h1>HEADER</h1>
footer.html
<h1>FOOTER</h1>
main1.html
<h1>MAIN1</h1>
main2.html
<h1>MAIN2</h1>
main.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()
}
After execution, visiting different route addresses will show different results:
