Golang template (text/template) (1)

  table of Contents

1. Basic concepts

Second, use steps

Three, simple template

3.1 Passing a single variable

3.2 Transfer structure

Four, summary


Golang templates provide a mechanism to fill a variable value into a text or HTML template. There are two packages that provide template functions, namely: text/template and html/template. The two packages have the same interface, the main difference is It is html/template that escapes some characters in order to prevent injection attacks.

This article mainly uses text/template as an example to explain. This article mainly introduces the basic concepts and usage steps of templates. More template skills and complex operations will be introduced in Golang template (text/template) (2) .

1. Basic concepts

First, briefly understand the concepts used in the template, and then explain through examples:

Actions: Data evaluation and control structure, wrapped with "{ {" and "}}";

Text: The text that can be output as it is in the template;

Arguments: value, can be boolean, integer, function, etc.;

Pipelines: chained sequence of commands;

Second, use steps

(1) Define the template

Define your own template according to your needs and use the form of a string to express, for example:

strTemplate := "I like {
   
   {.}}!\n" 

Among them, { {.}} is an Actions.

(2) Analysis template

Define the template name and parse the template, for example:

templ, err := template.New("templateName").Parse(strTemplate)

Among them, templateName is the template name.

(3) Data-driven template

Combine the data with the parsed template to produce output, for example:

err = templ.Execute(os.Stdout, variable) 

Three, simple template

3.1 Passing a single variable

First introduce the simple template for passing the value of a single variable. There is no complicated structure, which is convenient for quick understanding.

import (
	"os"
	"text/template"
)

func main() {
	name := "cats"                                               // 填充内容-可变
	strTemplate := "I like {
   
   {.}}!\n"                             // (1)定义模板-固定不变
	templ, err := template.New("templateName").Parse(strTemplate) // (2)解析模板
	if err != nil {
		panic(err)
	}
	err = templ.Execute(os.Stdout, name) //(3)数据驱动模板,将name的值填充到模板中
	if err != nil {
		panic(err)
	}
}

The output is:

I like cats!

The variable name will replace { {.}} in the template when the data-driven template in step (3) is used . Note whether the imported standard library is text/template or html/template. Of course, you can modify the value of name to output different results.

As can be seen from the above examples, { {.}} is used to refer to the element to be replaced in the template, where "." points to the current element, which is similar to the directory in Linux. After understanding the above, let’s look at another More complicated.

3.2 Transfer structure

Sometimes passing a single variable cannot meet the needs, and it is necessary to pass a structure. Let's see through an example.

import (
	"os"
	"text/template"
)

type student struct {
	Name string
	Like string
}

func main() {

	Joy := student{"Joy", "Ping pang"}
	strTemplate := "My name is  {
   
   {.Name}} and I like to play {
   
   {.Like}}!\n" // (1)定义模板-固定不变
	templ, err := template.New("templateOne").Parse(strTemplate)           // (2)解析模板
	if err != nil {
		panic(err)
	}
	err = templ.Execute(os.Stdout, Joy) //(3)数据驱动模板,将name的值填充到模板中
	if err != nil {
		panic(err)
	}
}

The output is:

My name is  Joy and I like to play Ping pang!

Use variable names such as .Name or .Like in the template to obtain the variables in the structure. When the data-driven template is passed into the corresponding structure in step (3), the corresponding value will be filled into the corresponding position of the template. Of course, if the variable in the structure is also a structure, you can use "." for further reference. For example, suppose Like in the above structure is also a structure and contains the variable Num, you can quote it like this: { {.Like.Num} }, there is no more code here.

Four, summary

From the above example, know how to use a template, you can define a simple template, and the value of the input data in the template is passed ".", if it is a nested structure, or use "." for further operations .

This article will be written here first, and the next article will explain in more depth.

Guess you like

Origin blog.csdn.net/u011074149/article/details/109019595