language standard library of template Go

html/templatePackage implements a data-driven templates for generating the security code injection may be against HTML output. It offers and text/templatepackages the same interface, scene Go language HTML output should use text/templatepackage.

template

In the MVC-based Web architecture, we usually need some data to back-end rendering HTML file in order to achieve the effect of dynamic web pages.

Template Example

The template is applied by a data structure (i.e., the data structure as a parameter template) is performed to obtain an output. Note reference template elements (such as ships or field structure of the key dictionary) data interface to control the execution and obtaining values ​​need to be rendered. Traverse the structure of the pointer value is performed when the template is represented as '' (referred to as "dot") points to the current position during operation of the data structure.

Used as a template input text must be utf-8 encoded text. "Action" - data arithmetic and control unit - is defined by "{{" and "}}"; all text outside of Action do not modified copy to the output. Action can not have internal wrap, but comments can have a wrap.

HTML file code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello</title>
</head>
<body>
<p>Hello {{.}}</p>
</body>
</html>
```

我们的HTTP server端代码如下:

// main.go

func sayHello(w http.ResponseWriter, r *http.Request) {
    // 解析指定文件生成模板对象
    tmpl, err := template.ParseFiles(&quot;./hello.html&quot;)
    if err != nil {
        fmt.Println(&quot;create template failed, err:&quot;, err)
        return
    }
    // 利用给定数据渲染模板,并将结果写入w
    tmpl.Execute(w, &quot;沙河小王子&quot;)
}
func main() {
    http.HandleFunc(&quot;/&quot;, sayHello)
    err := http.ListenAndServe(&quot;:9090&quot;, nil)
    if err != nil {
        fmt.Println(&quot;HTTP server failed,err:&quot;, err)
        return
    }
}

模板语法

{{.}}

模板语法都包含在{{}}中间,其中{{.}}中的点表示当前对象。

当我们传入一个结构体对象时,我们可以根据.来访问结构体的对应字段。例如:

// main.go

type UserInfo struct {
    Name   string
    Gender string
    Age    int
}

func sayHello(w http.ResponseWriter, r *http.Request) {
    // 解析指定文件生成模板对象
    tmpl, err := template.ParseFiles(&quot;./hello.html&quot;)
    if err != nil {
        fmt.Println(&quot;create template failed, err:&quot;, err)
        return
    }
    // 利用给定数据渲染模板,并将结果写入w
    user := UserInfo{
        Name:   &quot;小王子&quot;,
        Gender: &quot;男&quot;,
        Age:    18,
    }
    tmpl.Execute(w, user)
}

HTML文件代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello</title>
</head>
<body>
<p>Hello {{.Name}}</p>
<p>性别:{{.Gender}}</p>
<p>年龄:{{.Name}}</p>
</body>
</html>


<p>同理,当我们传入的变量是map时,也可以在模板文件中通过<code>.</code>根据key来取值。</p>

<h3 id="注释">注释</h3>

<pre><code class="language-template">{{/* a comment */}}
注释,执行时会忽略。可以多行。注释不能嵌套,并且必须紧贴分界符始止。

pipeline

pipeline是指产生数据的操作。比如{{.}}{{.Name}}等。Go的模板语法中支持使用管道符号|链接多个命令,用法和unix下的管道类似:|前面的命令会将运算结果(或返回值)传递给后一个命令的最后一个位置。

注意:并不是只有使用了|才是pipeline。Go的模板语法中,pipeline的概念是传递数据,只要能产生数据的,都是pipeline

变量

Action里可以初始化一个变量来捕获管道的执行结果。初始化语法如下:

$variable := pipeline


<p>其中$variable是变量的名字。声明变量的action不会产生任何输出。</p>

<h3 id="条件判断">条件判断</h3>

<p>Go模板语法中的条件判断有以下几种:</p>

<pre><code class="language-template">{{if pipeline}} T1 {{end}}

{{if pipeline}} T1 {{else}} T0 {{end}}

{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}

range

Go的模板语法中使用range关键字进行遍历,有以下两种写法,其中pipeline的值必须是数组、切片、字典或者通道。

{{range pipeline}} T1 {{end}}
如果pipeline的值其长度为0,不会有任何输出

{{range pipeline}} T1 {{else}} T0 {{end}}
如果pipeline的值其长度为0,则会执行T0。


<h3 id="with">with</h3>

<pre><code class="language-template">{{with pipeline}} T1 {{end}}
如果pipeline为empty不产生输出,否则将dot设为pipeline的值并执行T1。不修改外面的dot。

{{with pipeline}} T1 {{else}} T0 {{end}}
如果pipeline为empty,不改变dot并执行T0,否则dot设为pipeline的值并执行T1。

预定义函数

执行模板时,函数从两个函数字典中查找:首先是模板函数字典,然后是全局函数字典。一般不在模板内定义函数,而是使用Funcs方法添加函数到模板里。

预定义的全局函数如下:

and
函数返回它的第一个empty参数或者最后一个参数;
就是说"and x y"等价于"if x then y else x";所有参数都会执行;
or
返回第一个非empty参数或者最后一个参数;
亦即"or x y"等价于"if x then x else y";所有参数都会执行;
not
返回它的单个参数的布尔值的否定
len
返回它的参数的整数类型长度
index
执行结果为第一个参数以剩下的参数为索引/键指向的值;
如"index x 1 2 3"返回x[1][2][3]的值;每个被索引的主体必须是数组、切片或者字典。
print
即fmt.Sprint
printf
即fmt.Sprintf
println
即fmt.Sprintln
html
返回其参数文本表示的HTML逸码等价表示。
urlquery
返回其参数文本表示的可嵌入URL查询的逸码等价表示。
js
返回其参数文本表示的JavaScript逸码等价表示。
call
执行结果是调用第一个参数的返回值,该参数必须是函数类型,其余参数作为调用该函数的参数;
如"call .X.Y 1 2"等价于go语言里的dot.X.Y(1, 2);
其中Y是函数类型的字段或者字典的值,或者其他类似情况;
call的第一个参数的执行结果必须是函数类型的值(和预定义函数如print明显不同);
该函数类型值必须有1到2个返回值,如果有2个则后一个必须是error接口类型;
如果有2个返回值的方法返回的error非nil,模板执行会中断并返回给调用模板执行者该错误;


<h3 id="比较函数">比较函数</h3>

<p>布尔函数会将任何类型的零值视为假,其余视为真。</p>

<p>下面是定义为函数的二元比较运算的集合:</p>

<pre><code class="language-template">eq      如果arg1 == arg2则返回真
ne      如果arg1 != arg2则返回真
lt      如果arg1 &lt; arg2则返回真
le      如果arg1 &lt;= arg2则返回真
gt      如果arg1 &gt; arg2则返回真
ge      如果arg1 &gt;= arg2则返回真

为了简化多参数相等检测,eq(只有eq)可以接受2个或更多个参数,它会将第一个参数和其余参数依次比较,返回下式的结果:

{{eq arg1 arg2 arg3}}
```

比较函数只适用于基本类型(或重定义的基本类型,如”type Celsius float32”)。但是,整数和浮点数不能互相比较。

自定义函数

Go的模板支持自定义函数。

func sayHello(w http.ResponseWriter, r *http.Request) {
    htmlByte, err := ioutil.ReadFile(&quot;./hello.html&quot;)
    if err != nil {
        fmt.Println(&quot;read html failed, err:&quot;, err)
        return
    }
    // 自定义一个夸人的模板函数
    kua := func(arg string) (string, error) {
        return arg + &quot;真帅&quot;, nil
    }
    // 采用链式操作在Parse之前调用Funcs添加自定义的kua函数
    tmpl, err := template.New(&quot;hello&quot;).Funcs(template.FuncMap{&quot;kua&quot;: kua}).Parse(string(htmlByte))
    if err != nil {
        fmt.Println(&quot;create template failed, err:&quot;, err)
        return
    }

    user := UserInfo{
        Name:   &quot;小王子&quot;,
        Gender: &quot;男&quot;,
        Age:    18,
    }
    // 使用user渲染模板,并将结果写入w
    tmpl.Execute(w, user)
}

我们可以在模板文件hello.html中使用我们自定义的kua函数了。

{{kua .Name}}


<h3 id="嵌套template">嵌套template</h3>

<p>我们可以在template中嵌套其他的template。这个template可以是单独的文件,也可以是通过<code>define</code>定义的template。</p>

<p>举个例子:
t.html文件内容如下:</p>

<pre><code class="language-template">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;title&gt;tmpl test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    
    &lt;h1&gt;测试嵌套template语法&lt;/h1&gt;
    &lt;hr&gt;
    {{template &quot;ul.html&quot;}}
    &lt;hr&gt;
    {{template &quot;ol.html&quot;}}
&lt;/body&gt;
&lt;/html&gt;

{{ define &quot;ol.html&quot;}}
&lt;h1&gt;这是ol.html&lt;/h1&gt;
&lt;ol&gt;
    &lt;li&gt;吃饭&lt;/li&gt;
    &lt;li&gt;睡觉&lt;/li&gt;
    &lt;li&gt;打豆豆&lt;/li&gt;
&lt;/ol&gt;
{{end}}

ul.html文件内容如下:

<ul>
<li>注释</li>
<li>日志</li>
<li>测试</li>
</ul>
```

我们注册一个templDemo路由处理函数.

http.HandleFunc(&quot;/tmpl&quot;, tmplDemo)

tmplDemo函数的具体内容如下:

func tmplDemo(w http.ResponseWriter, r *http.Request) {
    tmpl, err := template.ParseFiles(&quot;./t.html&quot;, &quot;./ul.html&quot;)
    if err != nil {
        fmt.Println(&quot;create template failed, err:&quot;, err)
        return
    }
    user := UserInfo{
        Name:   &quot;小王子&quot;,
        Gender: &quot;男&quot;,
        Age:    18,
    }
    tmpl.Execute(w, user)
}

Guess you like

Origin www.cnblogs.com/nickchen121/p/11517448.html