Goweb template engine


template engine

Go provides us with two template engines: text / template library and HTML / template library
Engine generates the final HTML by combining the data and template, while the processor is responsible for calling the template engine and sending the
HTML generated by engine is returned to the client.

The templates of go are all text documents (the templates of web applications are usually HTML), and they are embedded
Some instructions are called actions. From the perspective of the template engine, a template is text embedded with actions (these texts
Usually included in the template file), and the template engine generates others by analyzing and executing the text


The web template engine using go requires two steps:

(1) The text format template source is parsed to create a parsed template structure
The template source can be either a string or something contained in the template file.

(2) execute the parsed template and transfer the dynamic data required by the responsewriter and template to the template
The board engine, which is called by the template engine, will combine the parsed template with the incoming data to generate the most
The final HTML and pass it to the responsewriter.

Let’s write a simple hello world

  1. Create template file hello.html
template file
    //Embed action {.}}
  1. Trigger template engine in processor
func handler(w http.ResponseWriter, r *http.Request) {
//Parsing template files
t, _ := template.ParseFiles("hello.html")
//Execution template
t.Execute(w, "Hello World!")
  1. Results in browser
    Hello World!

Analytical template

  1. Parsefiles function

When we call the parsefiles function to parse the template file, go will create a new template,
And take the name of the given template file as the name of the new template, if more than one template is passed in the function
File name, then only one template will be returned, and the file name of the first file will be used as the template
The template corresponding to other files will be placed in a map. Let’s do it again
Take a look at the code in HelloWorld:

t, _ := template.ParseFiles("hello.html")

The above code is equivalent to calling the new function to create a new template, and then calling the
Parsefiles method:

t := template.New("hello.html")
t, _ = t.ParseFiles("hello.html")

We didn’t handle the errors when parsing the template. Go provided a must function
The door handles this error. The must function can wrap a function, and the wrapped function will
Returns a pointer to the template and an error. If the error is not nil, the must function
A panic will be generated.

Code after experimenting with the must function

t := template.Must(template.ParseFiles("hello.html"))

  1. Parseglobe function

This function allows you to pass in multiple template files at one time by specifying a rule, such as:

t, _ := template.ParseGlob("*.html")

Execution template

  1. Through the execute method

If there is only one template file, it is always possible to call this method; however, if there are multiple templates
File, calling this method can only get the first template

  1. Through the executetemplate method

For example:

t, _ := template.ParseFiles("hello.html", "hello2.html")

The variable t is a collection of two templates. The first template is named
Hello.html. The name of the second template is hello.html. If you call the execute method directly,
Only the template hello.html will be executed. If you want to execute the template hello.html, you need to
Call executetemplate method

t. Executetemplate (W, "Hello 2. HTML", "I want to show it in Hello 2. HTML")

Recommended Today

[Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat:> keys * 1) “kkk” 2) “key1”> randomkey “key1”> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]