The template engine uses {{
and }}
as the default left and right delimiters. Developers can set custom template delimiters using the gview.SetDelimiters
method.
Use .
to access the value of the current object (template local variables).
Use $
to reference the root context of the current template (template global variables).
Use $var
to access specific template variables.
Supported go
language symbols in the template
{{"string"}} // General string
{{`raw string`}} // Raw string
{{'c'}} // Byte
{{print nil}} // nil is also supported
Pipeline in the template
It can be output from context variables or the return values passed through the pipeline from functions.
{{. | FuncA | FuncB | FuncC}}
When the value of the pipeline
is:
false
or0
nil pointer
orinterface
- Length of
0
forarray
,slice
,map
,string
Then this pipeline
is considered empty.
warning
Note: In the gf
template engine, when a specified variable shown in the template does not exist, it will appear empty (the standard library template engine would show <no value>
).
if … else … end
{{if pipeline}}...{{end}}
When if
evaluates, if the pipeline
is empty, it is equivalent to evaluating to false
.
Nested loops are supported.
{{if .condition}}
...
{{else}}
{{if .condition2}}
...
{{end}}
{{end}}
You can also use else if
{{if .condition}}
...
{{else if .condition2}}
...
{{else}}
...
{{end}}
range … end
{{range pipeline}} {{.}} {{end}}
The types supported by the pipeline
are slice
, map
, channel
.
Note: Inside the range
loop, the .
symbol will be overridden to the sub-elements of the above types (local variables). To access external variables (global variables) within the loop, add the $
symbol, such as {{$.Session.Name}}
.
Additionally, when the corresponding value length is 0
, range
will not execute, and .
will not change.
For example, to iterate a map
:
{{range $key, $value := .MapContent}}
{{$key}}:{{$value}}
{{end}}
For example, to iterate a slice
:
{{range $index, $elem := .SliceContent}}
{{range $key, $value := $elem}}
{{$key}}:{{$value}}
{{end}}
{{end}}
with … end
{{with pipeline}}...{{end}}
with
is used to redirect the pipeline
{{with .Field.NestField.SubField}}
{{.Var}}
{{end}}
define
define
can be used to customize template content blocks (define a template name for a section of template content). It can be used for module definition and template nesting (used in the template
tag).
{{define "loop"}}
<li>{{.Name}}</li>
{{end}}
Where loop
is the name of the template content block, which can subsequently be called using the template
tag:
<ul>
{{range .Items}}
{{template "loop" .}}
{{end}}
</ul>
warning
The define
tag needs to be used in conjunction with the template
tag and supports cross-template usage (effective within the same template directory/subdirectory, achieved by using the ParseFiles
method to parse template files).
template
{{template "templateName" pipeline}}
The corresponding context pipeline
is passed to the template for it to be called within the template.
Note: The parameter for the template
tag is templateName
, not the template file path. The template
tag does not support template file paths.
warning
The template
tag needs to be used in conjunction with the define
tag and supports cross-template usage (effective within the same template directory/subdirectory, achieved by using the ParseFiles
method to parse template files).
include
This is a new tag added for gf
framework template engines
{{include "templateFileName(with full file suffix)" pipeline}}
The include
tag can be used in the template to load other templates (any path). The template file name supports relative path as well as file system absolute path. To pass the template variables of the current template to the sub-template (nested template), you can do as follows:
{{include "templateFileName(with full file suffix)" .}}
The difference with the template
tag is that include
only supports file paths and does not support template names; whereas, the template
tag only supports template names and does not support file paths.
Comments
Multiline text comments are allowed but cannot be nested.
{{/*
comment content
support new line
*/}}
Remove Whitespace
Remove leading and trailing whitespace.
# Use {{- syntax to remove all whitespace on the left of the template content, and use -}} to remove all whitespace on the right of the template content.
# Note: - should be adjacent to {{ and }}, and there should be a space between the syntax and the template value.
{{- .Name -}}
{{- range $key, $value := .list}}
"{{$value}}"
{{- end}}