用golang写前端 -template

Go template

template模版

{{.}}是go中template的基本用法 .表示是当前对象,当我们使用结构体时,.后跟的就是结构体的字段

package main

import (
	"html/template"
	"log"
	"os"
)

type User struct {
	Name string
	Age  string
}

func templateFunc() {
	user := User{
		"lyizriii",
		"22",
	}
	temp := "hello,{{.Name}},age is {{.Age}}"
	//模版解析
	t, err := template.New("test").Parse(temp)
	if err != nil {
		log.Fatal(err)
	}
	//模版写入
	t.Execute(os.Stdout, user)
}

func main() {
	templateFunc()

}

运行后我们可以在终端看到

hello,lyizriii,age is 22%

结合html使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>Hello,{{.Name}},you age is {{.Age}}</div>
</body>
</html>
package main

import (
	"html/template"
	"log"
	"net/http"
)

type User struct {
	Name string
	Age  string
}

func templateFunc(w http.ResponseWriter, r *http.Request) {
	user := User{
		"lyizriii",
		"22",
	}
	//获取html文件并解析
	t, err := template.ParseFiles("test.html")
	if err != nil {
		log.Fatal(err)
	}
	t.Execute(w, user)
}
func tempHtmlWrite() {
    //开启端口
	serve := http.Server{
		Addr: "127.0.0.1:8080",
	}
	http.HandleFunc("/hello", templateFunc)
	serve.ListenAndServe()
}
func main() {
	tempHtmlWrite()
}

符号和空格

模版语法会讲所有的符号和空格原样保存下来,除了需要替换的地方以外,其他都按照文本格式保存下来,所以在非主观意愿下,请不要随意加入空格和符号,或者是缩进,换行

{{23}} < {{45}}. -> 23 < 45 {{23}}<{{45}} -> 23<45 {{23}}< {{45}} -> 23< 45 {{23-}} < {{-45}}-> 23<45

{{...-}} {{-...}} 会去除掉后面/前面的空格

pipeline(管道)

pipeline是指产生数据的操作 我们在前面讲到的{{.}}和{{.Name}}都属于pipeline,管道中也可以像是linux命令一样, |前面的命令会将运算结果(或返回值)传递给后一个命令的最后一个位置。

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

变量

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

$variable := pipeline
{{$Name := "tom"}}
{{$Name = "kite"}}
{{$Name}}

最后页面会显示kite 也就是前两行分别是定义、赋值,第三行才是展现数据

判断

语法条件判断一共有一下几种

{{if pipeline}} T1 {{end}}

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

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

为false的情况分别是 boo值为false int为0,float为0.0,指针、接口为nil,slice,array,map,string的len为0

条件表达式

eq(equl) arg1 == arg2 ne(not equl) arg1 != arg2 lt(less then) arg1 < arg2 le(less equl) arg1 <= arg2 gt(great then) arg1 > arg2 ge(great equl) arg1 >= arg2

{{$Age := 18}}
{{if (ge $Age 18)}}
<h3>成年人</h3>
{{else}}
<h3>未成年人</h3>
{{end}}

成年人

循环

 <div>
        {{range $x := .}}
            {{println $x}}
        {{end}}
</div>
	t, err := template.ParseFiles("test.html")
	if err != nil {
		log.Fatal(err)
	}
	s := []string{"hello,", "my name is", "lyizriii"}
	t.Execute(w, s)

hello,my name is lyizriii

with

{{with pipeline}} T1 {{end}}

如果pipeline为empty不产生输出,否则将dot设为pipeline的值并执行T1。不修改外面的dot。 {{with pipeline}} T1 {{else}} T0 {{end}} 如果pipeline为empty,不改变dot并执行T0,否则dot设为pipeline的值并执行T1。

{{with "hello"}}{{println .}}{{end}}

hello

预定义函数

执行模板时,函数从两个函数字典中查找:首先是模板函数字典,然后是全局函数字典。一般不在模板内定义函数,而是使用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明显不同);
    该函数类型值必须有12个返回值,如果有2个则后一个必须是error接口类型;
    如果有2个返回值的方法返回的error非nil,模板执行会中断并返回给调用模板执行者该错误;

猜你喜欢

转载自juejin.im/post/7111684812422774797