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

  1. {{"string"}} // General string
  2. {{`raw string`}} // Raw string
  3. {{'c'}} // Byte
  4. {{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.

  1. {{. | FuncA | FuncB | FuncC}}

When the value of the pipeline is:

  • false or 0
  • nil pointer or interface
  • Length of 0 for array, slice, map, string

Then this pipeline is considered empty.

Template Engine - Tags - 图1warning

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

  1. {{if pipeline}}...{{end}}

When if evaluates, if the pipeline is empty, it is equivalent to evaluating to false.

Nested loops are supported.

  1. {{if .condition}}
  2. ...
  3. {{else}}
  4. {{if .condition2}}
  5. ...
  6. {{end}}
  7. {{end}}

You can also use else if

  1. {{if .condition}}
  2. ...
  3. {{else if .condition2}}
  4. ...
  5. {{else}}
  6. ...
  7. {{end}}

range … end

  1. {{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:

  1. {{range $key, $value := .MapContent}}
  2. {{$key}}:{{$value}}
  3. {{end}}

For example, to iterate a slice:

  1. {{range $index, $elem := .SliceContent}}
  2. {{range $key, $value := $elem}}
  3. {{$key}}:{{$value}}
  4. {{end}}
  5. {{end}}

with … end

  1. {{with pipeline}}...{{end}}

with is used to redirect the pipeline

  1. {{with .Field.NestField.SubField}}
  2. {{.Var}}
  3. {{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).

  1. {{define "loop"}}
  2. <li>{{.Name}}</li>
  3. {{end}}

Where loop is the name of the template content block, which can subsequently be called using the template tag:

  1. <ul>
  2. {{range .Items}}
  3. {{template "loop" .}}
  4. {{end}}
  5. </ul>

Template Engine - Tags - 图2warning

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

  1. {{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.

Template Engine - Tags - 图3warning

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

  1. {{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:

  1. {{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.

  1. {{/*
  2. comment content
  3. support new line
  4. */}}

Remove Whitespace

Remove leading and trailing whitespace.

  1. # 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.
  2. # Note: - should be adjacent to {{ and }}, and there should be a space between the syntax and the template value.
  3. {{- .Name -}}
  4. {{- range $key, $value := .list}}
  5. "{{$value}}"
  6. {{- end}}

More Usage Instructions

template package - html/template - Go Packages

template package - text/template - Go Packages