The gview template engine supports two types of layout template layouts:

  1. define + template method
  2. include template 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.

Template Engine - Layout - 图1warning

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 template file extension.

Example Usage:

Template Engine - Layout - 图2

  1. layout.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>GoFrame Layout</title>
  5. {{template "header" .}}
  6. </head>
  7. <body>
  8. <div class="container">
  9. {{template "container" .}}
  10. </div>
  11. <div class="footer">
  12. {{template "footer" .}}
  13. </div>
  14. </body>
  15. </html>
  1. header.html
  1. {{define "header"}}
  2. <h1>{{.header}}</h1>
  3. {{end}}
  1. container.html
  1. {{define "container"}}
  2. <h1>{{.container}}</h1>
  3. {{end}}
  1. footer.html
  1. {{define "footer"}}
  2. <h1>{{.footer}}</h1>
  3. {{end}}
  1. main.go
  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request) {
  9. r.Response.WriteTpl("layout.html", g.Map{
  10. "header": "This is header",
  11. "container": "This is container",
  12. "footer": "This is footer",
  13. })
  14. })
  15. s.SetPort(8199)
  16. s.Run()
  17. }

After execution, visit http://127.0.0.1:8199 and the result is as follows:

Template Engine - Layout - 图3

include Template Embedding

Of course, we can also use the include tag to achieve page layout.

Template Engine - Layout - 图4warning

Note: When passing template variables to a nested child template, use the syntax: {{include "xxx" .}}.

Example Usage:

Template Engine - Layout - 图5

  1. layout.html
  1. {{include "header.html" .}}
  2. {{include .mainTpl .}}
  3. {{include "footer.html" .}}
  1. header.html
  1. <h1>HEADER</h1>
  1. footer.html
  1. <h1>FOOTER</h1>
  1. main1.html
  1. <h1>MAIN1</h1>
  1. main2.html
  1. <h1>MAIN2</h1>
  1. main.go
  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/main1", func(r *ghttp.Request) {
  9. r.Response.WriteTpl("layout.html", g.Map{
  10. "mainTpl": "main/main1.html",
  11. })
  12. })
  13. s.BindHandler("/main2", func(r *ghttp.Request) {
  14. r.Response.WriteTpl("layout.html", g.Map{
  15. "mainTpl": "main/main2.html",
  16. })
  17. })
  18. s.SetPort(8199)
  19. s.Run()
  20. }

After execution, visiting different route addresses will show different results:

  1. http://127.0.0.1:8199/main1

Template Engine - Layout - 图6

  1. http://127.0.0.1:8199/main2

Template Engine - Layout - 图7