go语言的模板,text/template包

定义

模板就是将一组文本嵌入另一组文本里

传入string--最简单的替换

package main

import (
"os"
"text/template"
) func main() {
name := "waynehu"
tmpl, err := template.New("test").Parse("hello, {{.}}") //建立一个模板,内容是"hello, {{.}}"
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, name) //将string与模板合成,变量name的内容会替换掉{{.}}
//合成结果放到os.Stdout里
if err != nil {
panic(err)
}
}
//输出 : hello, waynehu

因为"hello, {{.}}"也是一个字符串,所以可以单独拎出来,如下:

//这句
tmpl, err := template.New("test").Parse("hello, {{.}}")
//等于下面的两句
muban := "hello, {{.}}"
tmpl, err := template.New("test").Parse(muban)
//之后的例子都用两句的方式表达

传入struct

模板合成那句,第2个参数是interface{},所以可以传入任何类型,现在传入struct看看 要取得struct的值,只要使用成员名字即可,看代码吧:

package main

import (
"os"
"text/template"
) type Inventory struct {
Material string
Count uint
} func main() {
sweaters := Inventory{"wool", 17}
muban := "{{.Count}} items are made of {{.Material}}"
tmpl, err := template.New("test").Parse(muban) //建立一个模板
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, sweaters) //将struct与模板合成,合成结果放到os.Stdout里
if err != nil {
panic(err)
}
}
//输出 : 17 items are made of wool

多模板,介绍New,Name,Lookup

//一个模板可以有多种,以Name来区分
muban_eng := "{{.Count}} items are made of {{.Material}}"
muban_chn := "{{.Material}}做了{{.Count}}个项目"
//建立一个模板的名称是china,模板的内容是muban_chn字符串
tmpl, err := template.New("china")
tmpl, err = tmpl.Parse(muban_chn)
//建立一个模板的名称是english,模板的内容是muban_eng字符串
tmpl, err = tmpl.New("english")
tmpl, err = tmpl.Parse(muban_eng)
//将struct与模板合成,用名字是china的模板进行合成,结果放到os.Stdout里,内容为“wool做了17个项目”
err = tmpl.ExecuteTemplate(os.Stdout, "china", sweaters)
//将struct与模板合成,用名字是china的模板进行合成,结果放到os.Stdout里,内容为“17 items are made of wool”
err = tmpl.ExecuteTemplate(os.Stdout, "english", sweaters) tmpl, err = template.New("english")
fmt.Println(tmpl.Name()) //打印出english
tmpl, err = tmpl.New("china")
fmt.Println(tmpl.Name()) //打印出china
tmpl=tmpl.Lookup("english")//必须要有返回,否则不生效
fmt.Println(tmpl.Name()) //打印出english

文件模板,介绍ParseFiles

//模板可以是一行
muban := "{{.Count}} items are made of {{.Material}}"
//也可以是多行
muban := `items number is {{.Count}}
there made of {{.Material}}
`

把模板的内容发在一个文本文件里,用的时候将文本文件里的所有内容赋值给muban这个变量即可
上面的想法可以自己实现,但其实tamplate包已经帮我们封装好了,那就是template.ParseFiles方法

假设有一个文件mb.txt的内容是muban变量的内容
$cat mb.txt
{{.Count}} items are made of {{.Material}} 那么下面2行
muban := "{{.Count}} items are made of {{.Material}}"
tmpl, err := template.New("test").Parse(muban) //建立一个模板 等价于
tmpl, err := template.ParseFiles("mb.txt") //建立一个模板,这里不需要new("name")的方式,因为name自动为文件名

文件模板,介绍ParseGlob

ParseFiles接受一个字符串,字符串的内容是一个模板文件的路径(绝对路径or相对路径)
ParseGlob也差不多,是用正则的方式匹配多个文件

假设一个目录里有a.txt b.txt c.txt的话
用ParseFiles需要写3行对应3个文件,如果有一万个文件呢?
而用ParseGlob只要写成template.ParseGlob("*.txt") 即可

模板的输出,介绍ExecuteTemplate和Execute

模板下有多套模板,其中有一套模板是当前模板
可以使用Name的方式查看当前模板

err = tmpl.ExecuteTemplate(os.Stdout, "english", sweaters)  //指定模板名,这次为english
err = tmpl.Execute(os.Stdout, sweaters) //模板名省略,打印的是当前模板

模板的复用

模板里可以套模板,以达到复用目的,用template关键字

muban1 := `hi, {{template "M2"}},
hi, {{template "M3"}}
`
muban2 := "我是模板2,{{template "M3"}}"
muban3 := "ha我是模板3ha!" tmpl, err := template.New("M1").Parse(muban1)
tmpl.New("M2").Parse(muban2)
tmpl.New("M3").Parse(muban3)
err = tmpl.Execute(os.Stdout, nil)

完整代码:

package main

import (
"os"
"text/template"
) func main() {
muban1 := `hi, {{template "M2"}},
hi, {{template "M3"}}
`
muban2 := `我是模板2,{{template "M3"}}`
muban3 := "ha我是模板3ha!" tmpl, err := template.New("M1").Parse(muban1)
if err != nil {
panic(err)
}
tmpl.New("M2").Parse(muban2)
if err != nil {
panic(err)
}
tmpl.New("M3").Parse(muban3)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, nil)
if err != nil {
panic(err)
}
}

输出的内容

hi, 我是模板2,ha我是模板3ha!,
hi, ha我是模板3ha!

模板的回车

模板文件里的回车也是模板的一部分,如果对回车位置控制不好,合成出来的文章会走样 标准库里的Example(Template)写的还是有点乱,我整理如下:

const letter = `Dear {{.Name}},

{{if .Attended}}It was a pleasure to see you at the wedding.
如果Attended是true的话,这句是第二行{{else}}It is a shame you couldn't make it to the wedding.
如果Attended是false的话,这句是第二行{{end}}
{{with .Gift}}Thank you for the lovely {{.}}.
{{end}}
Best wishes,
Josie `

解释一下:

  • Dear某某某的Dear应该是在第一行,所以在D前面不能有回车,否则Dear会跑到第2行去

    • 所以Dear要紧贴```
  • 信件的称唿和正文有一行空行,最好显式的打出一行,而标准库里的回车是包在if里,成为正文的一部分,这样排版容易出错
  • 正确的正文排版如下
    • 如果正文就一行,要把true和false的所有内容都写在一行

      • 比如{{if .Attended}}true line,hello true{{else}}false line,hi false{{end}}
    • 如果正文有多行,就等于把一行拆成多行
      • 会发现true的最后一行和false的第一行是在同一行
      • {{if .Attended}}和ture的第一行在同一行
      • {{end}}和false的最后一行在同一行

如下

{{if .Attended}}true line
hello true{{else}}false line
hi false{{end}}
  • 关于{{with .Gift}},意思是如果Gift不是为空的话,就打印整行,如果为空,就不打印

    • 只有这样写法,with对应的end要写在第2行,才会把“Thank you”这句后面带一个回车进去,这样写法,就像“Thank you”这句是插在正文下面的
    • 只有这样写,不管有没有“Thank you”,正文和Best wishes,之间始终只有1行空白

go语言的模板,text/template包的更多相关文章

  1. [golang]text/template模板

    这个可以用来处理text文本,不过我更偏爱做成代码生成器. [golang]text/template模板 package main import ( "os" "tex ...

  2. golang 模板 html/template与text/template

    html模板生成: html/template包实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.它提供了和text/template包相同的接口,Go语言中输出HTML的场景都应使用t ...

  3. go语言template包中模板语法总结

    package main; import ( "html/template" "os" "fmt" ) type Person struct ...

  4. 前端模板<script type="text/template" id="tmpl">

    前端模板, 比连接字符串好用多了, 还可以使用循环\判断等语句, 减少工作量 <script type="text/template" id="member-tmp ...

  5. 关于MVC模板渲染的一点小事type="text/template"

    先上一个demo,简单粗暴,请自便 <!DOCTYPE html> <html> <head lang="en"> <meta chars ...

  6. go标准库的学习-text/template

    参考:https://studygolang.com/pkgdoc 导入方式: import "text/template" template包实现了数据驱动的用于生成文本输出的模 ...

  7. Text Template Transformation Toolkit

    Text Template Transformation Toolkit       1.且算简介         笔者以一个英文字母和一个数字取了一个简单的名字.名唤"T4"(名 ...

  8. Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)

    前言:当我们想在页面上给客户端返回一个当前时间,一些初学者可能会很自然的想到用占位符,字符串拼接来达到我们想要的效果,但是这样做会有一个问题,HTML被直接硬编码在 Python代码之中. 1 2 3 ...

  9. DjangoMTV模型之视图层views及模板层template

    Django视图层中views的内容 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容(render),也可以是一个重定向( ...

随机推荐

  1. 记一次ftp服务器搭建走过的坑

    记一次ftp服务器搭建走过的坑 1.安装 ①下载 wget https://security.appspot.com/downloads/vsftpd-3.0.3.tar.gz #要FQ ②解压 ta ...

  2. JS判断浏览器类型及版本

    浏览器 ie firefox opera safari chrome 分类: 一路辛酸---JavaScript 你知道世界上有多少种浏览器吗?除了我们熟知的IE, Firefox, Opera, S ...

  3. Lua面向对象编程

    Lua中的table就是一种对象,看以下一段简单的代码: , b = } , b = } local tb3 = tb1 if tb1 == tb2 then print("tb1 == t ...

  4. MSBI BigData demo—sqoop import

    --sp_readerrorlog 读取错误的信息记录 exec sys.sp_readerrorlog 0, 1, 'listening'查看端口号 首先hadoop环境要配置完毕,并检验可以正常启 ...

  5. uva 1471 defence lines——yhx

    After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...

  6. 我的opencv之旅:ios人脸识别

    学习opencv有一年多了,这本来是我的毕业设计的一部分,但是因为不能突出专业重点,所以换了个课题. opencv在vc.android.ios下都能用,其中vc和android下的教程和主题贴最多, ...

  7. 51nod-1537 1537 分解(矩阵快速幂+找规律)

    题目链接: 1537 分解  问(1+sqrt(2)) ^n  能否分解成 sqrt(m) +sqrt(m-1)的形式  如果可以 输出 m%1e9+7 否则 输出no Input 一行,一个数n.( ...

  8. HDU 3667 费用流(拆边)

    题意:有n个城市(1~n),m条有向边:有k件货物要从1运到n,每条边最多能运c件货物,每条边有一个危险系数ai,经过这条路的费用需要ai*x2(x为货物的数量),问所有货物安全到达的费用. 思路:c ...

  9. Java学习----Java概述

    一.常用DOS命令 d:      盘符切换 dir(directory)  列出当前目录下的文件以及文件夹 md (make directory)  创建目录 rd (remove director ...

  10. [转] Centos 6.4 python 2.6 升级到 2.7

    http://blog.csdn.net/jcjc918/article/details/11022345