Skip to main content
Version: 2.8.x(Latest)

The view component is one of the core components of the GoFrame framework, and it also supports very convenient configuration management.

Configuration Object

Configuration object definition: https://pkg.go.dev/github.com/gogf/gf/v2/os/gview#Config

Configuration File

The view component supports configuration files. When using g.View(singleton name) to get the View singleton object, it automatically retrieves the corresponding View configuration through the default configuration management object. By default, it reads the viewer.singleton name configuration item. If this configuration item does not exist, it reads the viewer configuration item.

The complete configuration file items and descriptions are as follows, where configuration item names are case-insensitive:

[viewer]
Paths = ["/var/www/template"] # Template file search directory path, it is recommended to use absolute paths. Default is the current program's working path
DefaultFile = "index.html" # Default template engine file for parsing. Default is "index.html"
Delimiters = ["${", "}"] # Template engine variable delimiters. Default is ["{{", "}}"]
AutoEncode = false # Whether to perform XSS encoding on variable content by default. Default is false
[viewer.Data] # Custom global Key-Value pairs, which can be directly used in template parsing
Key1 = "Value1"
Key2 = "Value2"

Example 1, Default Configuration Items

[viewer]
paths = ["template", "/var/www/template"]
defaultFile = "index.html"
delimiters = ["${", "}"]
[viewer.data]
name = "gf"
version = "1.10.0"

Then, you can use g.View() to automatically get and set this configuration when obtaining the default singleton object.

Example 2, Multiple Configuration Items

Example of configuration for multiple View objects:

[viewer]
paths = ["template", "/var/www/template"]
defaultFile = "index.html"
delimiters = ["${", "}"]
[viewer.data]
name = "gf"
version = "1.10.0"
[viewer.view1]
defaultFile = "layout.html"
delimiters = ["${", "}"]
[viewer.view2]
defaultFile = "main.html"
delimiters = ["#{", "}"]

We can obtain the corresponding View singleton object configuration through the singleton object name:

// Corresponds to viewer.view1 configuration item
v1 := g.View("view1")
// Corresponds to viewer.view2 configuration item
v2 := g.View("view2")
// Corresponds to the default configuration item viewer
v3 := g.View("none")
// Corresponds to the default configuration item viewer
v4 := g.View()

Configuration Methods

Method list: https://pkg.go.dev/github.com/gogf/gf/v2/os/gview

Brief introduction:

  1. You can set using SetConfig and SetConfigWithMap.
  2. You can also use the Set* method of the View object for specific configuration settings.
  3. It is crucial to set the configuration items before the View object executes template parsing to avoid concurrency safety issues.

SetConfigWithMap Method

We can use the SetConfigWithMap method to set/modify specific configurations of the View using Key-Value pairs, while the remaining configurations use the default configuration. The Key name is the attribute name in the Config struct, which is case-insensitive, and it supports using -/ _/ space symbols between words. Refer to the Type Conversion - Struct section for conversion rules.

Simple example:

package main

import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gview"
)

func main() {
view := gview.New()
view.SetConfigWithMap(g.Map{
"Paths": []string{"template"},
"DefaultFile": "index.html",
"Delimiters": []string{"${", "}"},
"Data": g.Map{
"name": "gf",
"version": "1.10.0",
},
})
result, err := view.ParseContent(context.TODO(), "hello ${.name}, version: ${.version}")
if err != nil {
panic(err)
}
fmt.Println(result)
}

Here, DefaultFile represents the default template file for parsing. The key name can also be defaultFile, default-File, default_file, default file, and other configuration properties can be deduced similarly.

Notes

It's common for users to ask, why isn't my template parsing effective? Why does the page directly display the tags I wrote as is?

At this time, please check whether your configuration file has set the template tags. A common scenario is setting delimiters to ["${", "}"], but using ["{{", "}}"] in the template.