Non-controller view
The use of the template engine in a non-controller is not supported by the Controller view and can be implemented using the underlying Gview package, which can be used to obtain the default Singleton Gview object using the Singleton manager.
Gview Package Method List:
func Get(path string) *Viewfunc New(path string) *Viewfunc (view *View) BindFunc(name string, function interface{})func (view *View) Parse(file string, params map[string]interface{}) ([]byte, error)func (view *View) ParseContent(content string, params map[string]interface{}) ([]byte, error)func (view *View) GetPath() stringfunc (view *View) SetPath(path string)
Using Example 1:
package mainimport ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gins")func main() { s := ghttp.GetServer() s.BindHandler("/template2", func(r *ghttp.Request){ content, _ := gins.View().Parse("index.tpl", map[string]interface{}{ "id" : 123, "name" : "john", }) r.Response.Write(content) }) s.SetPort(8199) s.Run()}
In this example we use the Singleton Manager to get a default view object, which is then used to render the template file under the corresponding template directory index.tpl
and given the template variable parameters.
We can also SetPath
specify the template directory of the View object through the method center, which is concurrency-safe, but it is important to note that once the template directory for that view object is changed, it will take effect throughout the process.
Of course, you can also parse the template content directly, using Example 2:
package mainimport ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gins")func main() { s := ghttp.GetServer() s.BindHandler("/template2", func(r *ghttp.Request){ tplcontent := `id:{{.id}}, name:{{.name}}` content, _ := gins.View().ParseContent(tplcontent, map[string]interface{}{ "id" : 123, "name" : "john", }) r.Response.Write(content) }) s.SetPort(8199) s.Run()}
After execution, the access http://127.0.0.1:8199/template2
can see the parsed content as:id:123, name:john
Also, it is important to note that while the above examples are presented in Web server, the template engine is an intelligent parsing of template files/content that can be used in any scenario.
Modify Template Catalog
As the core component of the GF framework, the template engine can modify the template engine's default template file lookup directory in the following ways:
- (recommended) Singleton mode gets the global view object, which is
SetPath
manually modified by method
- Modify command-line startup Parameters-
viewpath
- Modifies the specified environment variable-
gf.viewpath
For example, our execution program file is main, so you can modify the template directory (under Linux) in the following ways:
(recommended) By single-case mode
gins.View().SetPath("/opt/template")
Through command-line arguments
./main --viewpath=/opt/template/
Through environment variables
Modify environment variables at startup:
gf.viewpath=/opt/config/; ./main
Use the GENV package to modify environment variables:
genv.Set("gf.viewpath", "/opt/template")
Automatically detect updates
The template engine uses a caching mechanism that, when the template file is read for the first time, is cached to memory, and the next read is fetched directly from the cache to improve execution efficiency. Also, the template engine provides a mechanism for automatic detection of template files, and when the template file is externally modified, the template engine can instantly monitor and refresh the cached content of the template file.
The automatic detection and update mechanism of the template engine is also a feature of the GF framework.