Skip to main content

Template Content Return

While Writef can format data into HTML content, this approach becomes cumbersome when dealing with extensive data and HTML files. Therefore, web frameworks typically employ template engines using template languages to render data, simplifying the interaction between HTML pages and backend data.

In GoFrame, use the following methods for template parsing and return:

func (r *Response) WriteTpl(tpl string, params ...gview.Params) error 
func (r *Response) WriteTplDefault(params ...gview.Params) error
func (r *Response) WriteTplContent(content string, params ...gview.Params) error

The most commonly used method is WriteTpl. More details about template engines will be discussed later, but for now, understand that this method reads an HTML file and returns it to the client. By default, template files are located under resource/template, so the first parameter of WriteTpl is the relative path to the template HTML file relative to template.

Example:

req.Response.WriteTpl("index.html")
req.Response.WriteTpl("user/index.html")