Template Engine
Previously, we mentioned several methods for returning template content as follows:
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error
func (r *Response) WriteTplDefault(params ...gview.Params) error
func (r *Response) WriteTplContent(content string, params ...gview.Params) error
By default, the template directory is resource/template
.
WriteTpl
parses and returns the content of the template file located relative toresource/template
.WriteTplDefault
parses and returns the default template located atresource/template/index.html
.WriteTplContent
parses and returns the template string.
The commonly used method is WriteTpl
, while the other two methods are for simple understanding.
func (c *Controller) Tpl(req *ghttp.Request) {
req.Response.WriteTplDefault() // Parses and returns the default template file content
// Parses and returns the template string
req.Response.WriteTplContent("<h1>Hello, {{.name}} welcome to learn {{.lang}}</h1>", g.Map{"name": "Wang Daochang", "lang": "GoFrame"})
}
Simple Usage Example
resource/template/hello/index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<h1>Hello, {{.name}}</h1>
<h2>Welcome to the {{.lesson}} course</h2>
<p>This course has {{.num}} lessons. Currently learning {{.what}}</p>
</div>
</body>
</html>
controller
func (c *Controller) Tpl(req *ghttp.Request) {
data := g.Map{
"name": "Wang Yaodao",
"lesson": "Introduction to GoFrame",
"num": 5,
"what": "Template engine usage example",
}
req.Response.WriteTpl("hello/index.html", data)
}
Template Configuration
In general, the default configuration is sufficient for template usage. If modifications are needed, they can be made in manifest/config/config.yaml
.
viewer: # Template configuration
paths: ["resource/template", "/www/template"] # Template path configuration, can configure multiple paths
defaultFile: "index.hmtl" # File parsed by WriteTplDefault
delimiters: ["${", "}"] # Template engine variable delimiters, default is ["{{", "}}"]
Static Resources
Static resources are not part of the template engine content, but they are often needed in template files. Therefore, here is some additional information.
Static resources typically refer to JS/CSS/image files or static HTML files. In GoFrame projects, these files are placed in resource/public
. To reference these resources in template files, you need to enable static resource serving. There are two ways to do this:
- Configuration file
manifest/config/config.yaml
server:
serverRoot: "resource/public"
indexFolder: true # This can be omitted, here for understanding
- Enable via code
internal/cmd/cmd.go
s := g.Server()
s.SetServerRoot("resource/public")
s.SetIndexFolder(true)
The serverRoot
configures the root directory for static resources (resource/public
). Resource URLs are relative to resource/public
.
For example, if resource/public/resource/css/bootstrap.css
contains a file, it is referenced as follows:
<link rel="stylesheet" href="/resource/css/bootstrap.css">
Conditional Statements
Conditional statements can be used in templates to display different content based on whether conditions are met. The syntax is as follows:
{{if .condition}}
Display content when condition is true
{{else}}
Display content when condition is false
{{end}}
You can nest and chain multiple {{else if .condition}}
statements. The condition is false if .condition
is empty (0
, ""
, nil
, etc.), otherwise true.
Example:
resource/template/hello/index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/resource/css/bootstrap.css">
<title>Document</title>
</head>
<body>
{{if .name}}
<h1>Value of name: {{.name}}</h1>
{{else}}
<h1>Value of name is false/""/0/nil, etc.</h1>
{{end}}
</body>
</html>
controller
func (c *Controller) Tpl(req *ghttp.Request) {
data := g.Map{
"name": "Wang Yaodao",
"lesson": "Introduction to GoFrame",
"num": 5,
"what": "Template engine usage example",
}
req.Response.WriteTpl("hello/index.html", data)
}
Comparison Operators
Direct use of >
, <
, ==
, etc., for comparison is not supported in templates. Instead, use conditional functions:
Function | Equivalent |
---|---|
eq | == |
ne | != |
lt | < |
le | <= |
gt | > |
ge | >= |
Usage example:
{{if eq 5 .num}}
<h1>num == 5</h1>
{{else if lt 5 .num}}
<h1>num > 5</h1>
{{else}}
<h1>num < 5</h1>
{{end}}
There are additional uses for these functions, but we cover the basics here.
Logical Operators
Logical operators and
, or
, not
can be used for logical operations in templates:
{{if and (gt .num 0) (lt .num 5)}} if num > 0 && num < 5
{{if or (eq .num 0) (eq .num 5)}} if num == 0 || num == 5
{{if not (eq .num 0)}} if num != 0
Loops
range ... end
Looping through slices
controller/hello.go
data := g.Map{
"slice": g.Array{1, 2, 3, "Zhang Chulan", "Zhuge Qing"},
}
req.Response.WriteTpl("hello/index.html", data)
index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/resource/css/bootstrap.css">
<title>Document</title>
</head>
<body>
{{range .slice}}
<span>{{.}}</span>
{{end}}
{{range $index, $value := .slice}}
<p>index = {{$index}}, value = {{$value}}</p>
{{end}}
</body>
</html>
Using map data
controller/hello.go
func (c *Controller) Tpl(req *ghttp.Request) {
data := g.Map{
"mp": g.Map{
"name": "Feng Baobao",
"gender": "Female",
"age": 100,
"hobby": "Burying people",
},
}
req.Response.WriteTpl("hello/index.html", data)
}
index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/resource/css/bootstrap.css">
<title>Document</title>
</head>
<body>
<div class="container">
<p>Name: {{.mp.name}}</p>
<p>Gender: {{.mp.gender}}</p>
<p>Age: {{.mp.age}}</p>
<p>Hobby: {{.mp.hobby}}</p>
{{range .mp}}
<p>{{.}}</p>
{{end}}
{{range $key, $value := .mp}}
<p>{{$key}}: {{$value}}</p>
{{end}}
</div>
</body>
</html>