go语言的模板,text/template包
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的最后一行在同一行
- 如果正文就一行,要把true和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包的更多相关文章
- [golang]text/template模板
这个可以用来处理text文本,不过我更偏爱做成代码生成器. [golang]text/template模板 package main import ( "os" "tex ...
- golang 模板 html/template与text/template
html模板生成: html/template包实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.它提供了和text/template包相同的接口,Go语言中输出HTML的场景都应使用t ...
- go语言template包中模板语法总结
package main; import ( "html/template" "os" "fmt" ) type Person struct ...
- 前端模板<script type="text/template" id="tmpl">
前端模板, 比连接字符串好用多了, 还可以使用循环\判断等语句, 减少工作量 <script type="text/template" id="member-tmp ...
- 关于MVC模板渲染的一点小事type="text/template"
先上一个demo,简单粗暴,请自便 <!DOCTYPE html> <html> <head lang="en"> <meta chars ...
- go标准库的学习-text/template
参考:https://studygolang.com/pkgdoc 导入方式: import "text/template" template包实现了数据驱动的用于生成文本输出的模 ...
- Text Template Transformation Toolkit
Text Template Transformation Toolkit 1.且算简介 笔者以一个英文字母和一个数字取了一个简单的名字.名唤"T4"(名 ...
- Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)
前言:当我们想在页面上给客户端返回一个当前时间,一些初学者可能会很自然的想到用占位符,字符串拼接来达到我们想要的效果,但是这样做会有一个问题,HTML被直接硬编码在 Python代码之中. 1 2 3 ...
- DjangoMTV模型之视图层views及模板层template
Django视图层中views的内容 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容(render),也可以是一个重定向( ...
随机推荐
- java Memorymapfile demo
String lineseperator = java.security.AccessController .doPrivileged(new sun.security.action.GetPrope ...
- quick sort java version
import java.util.Random; public class test { public static void main(String[] args) { int[] arr= gen ...
- 《MapReduce: Simplified Data Processing on Large Cluster 》翻译
Abstract MapReduce是一种编程模型和一种用来处理和产生大数据集的相关实现.用户定义map函数来处理key/value键值对来产生一系列的中间的key/value键值对.还要定义一个re ...
- 读《深入理解Java虚拟机》有感——第二部分:虚拟机类加载机制
一.类加载过程 执行时机:编译程序——>执行程序(JVM启动.程序运行),类加载发生在程序运行期间 各个阶段:分为加载阶段.连接阶段(验证.准备.解析).初始化.使用.卸 ...
- AC日记——搞笑世界杯 codevs 1060(dp)
题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...
- 注册其它地区Apple ID
不同地区Apple Id需求 iPhone 最近换上了iPhone,想要下载其它国家地区的游戏,需要登录该国的Apple Id才能在itunes里下载. 下面记录一下我注册日本地区的Apple Id过 ...
- 委托--delegate
委托,跟类很相似,能够定义对象,但是区别是 1,委托必须有关键字delegate. 2,委托有类型修饰符,比如void,string,int.修饰符取决于他的方法返回类型. 3,委托没有方法实现. d ...
- jQuery Ajax 操作函数及deferred对象
jQuery Ajax 操作函数 jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 函数 描述 jQuery.ajax() 执行异步 H ...
- swift为UIView添加extension扩展frame
添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...
- linux64需要增加的依赖库
sudo apt-get install git-core gnupg flex bison gperf build-essential \ zip curl zlib1g-dev gcc-multi ...