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:

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

Example 1, Default Configuration Items

  1. [viewer]
  2. paths = ["template", "/var/www/template"]
  3. defaultFile = "index.html"
  4. delimiters = ["${", "}"]
  5. [viewer.data]
  6. name = "gf"
  7. 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:

  1. [viewer]
  2. paths = ["template", "/var/www/template"]
  3. defaultFile = "index.html"
  4. delimiters = ["${", "}"]
  5. [viewer.data]
  6. name = "gf"
  7. version = "1.10.0"
  8. [viewer.view1]
  9. defaultFile = "layout.html"
  10. delimiters = ["${", "}"]
  11. [viewer.view2]
  12. defaultFile = "main.html"
  13. delimiters = ["#{", "}"]

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

  1. // Corresponds to viewer.view1 configuration item
  2. v1 := g.View("view1")
  3. // Corresponds to viewer.view2 configuration item
  4. v2 := g.View("view2")
  5. // Corresponds to the default configuration item viewer
  6. v3 := g.View("none")
  7. // Corresponds to the default configuration item viewer
  8. 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:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gview"
  7. )
  8. func main() {
  9. view := gview.New()
  10. view.SetConfigWithMap(g.Map{
  11. "Paths": []string{"template"},
  12. "DefaultFile": "index.html",
  13. "Delimiters": []string{"${", "}"},
  14. "Data": g.Map{
  15. "name": "gf",
  16. "version": "1.10.0",
  17. },
  18. })
  19. result, err := view.ParseContent(context.TODO(), "hello ${.name}, version: ${.version}")
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(result)
  24. }

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.