html/template 和 text/template区别
文章目录
- 前言
- html/template
- text/template
前言
最近在学习go template,跟着一篇文章进行例程的学习,结果发现无论怎么调试,都没有办法复现例程的打印结果
纠结了一天后,求助同事发现了其中的端倪
text/template
是将内容都已text文本格式返回。
html/tempalte
针对的是需要返回HTML内容的场景。
在模板渲染过程中会对一些有风险的内容进行转义,以此来防范跨站脚本攻击。
html/template
例程中给出的代码如下
package mainimport ("fmt"// "text/template""html/template"
)func main() {// ******ParseFiles()方法*****t1 := template.New("test")t1, err := t1.ParseFiles("a.cnf", "b.cnf", "c.cnf")// ******ParseFiles()定义*****// t1, err := template.ParseFiles("a.cnf", "b.cnf", "c.cnf")if err != nil {panic(err)}// 先注释下面这行//t1.Parse("")fmt.Println(t1.DefinedTemplates())fmt.Println()fmt.Println(t1)fmt.Println(t1.Lookup("a.cnf"))fmt.Println(t1.Lookup("b.cnf"))fmt.Println(t1.Lookup("c.cnf"))
}
理论打印结果应该是:
但是可以看到我们跑程序后打印结果如下:
出现了很大的不一致,这时,我百思不得起解,无论怎么样单步调试,修改输出,都没有办法得到例程输出
最后在同事的提醒下,查看函数的源码,直接F12
首先是Template.New()
// New allocates a new HTML template with the given name.
func New(name string) *Template {ns := &nameSpace{set: make(map[string]*Template)}ns.esc = makeEscaper(ns)tmpl := &Template{nil,template.New(name),nil,ns,}tmpl.set[name] = tmplreturn tmpl
}
New返回tmpl,tmpl结构体中我们可以改变的是name,也就是存储我们输入的“test”
这里暂时看不出来端倪,继续看后面的t1.DefinedTemplates()
,可以发现也正常,这块输出确实也是对的
func (t *Template) DefinedTemplates() string {return t.text.DefinedTemplates()
}
那么问题出在哪里呢?
随着调试过程逐渐深入,发现端倪或许在Template
中
进入到Template
,可以看如下结构体
// Template is a specialized Template from "text/template" that produces a safe
// HTML document fragment.
type Template struct {// Sticky error if escaping fails, or escapeOK if succeeded.escapeErr error// We could embed the text/template field, but it's safer not to because// we need to keep our version of the name space and the underlying// template's in sync.text *template.Template// The underlying template's parse tree, updated to be HTML-safe.Tree *parse.Tree*nameSpace // common to all associated templates
}
无论如何,我们的fmt.Println(t1)
打印代码也是按照顺序打印Template
结构体,结构体中第一个是erro,第二个是template.Template,第三个是parse.Tree,第四个是*nameSpace
这样也就能解释为什么输出结果是
&{<nil> 0xc00007e0c0 0xc0001085a0 0xc00005c180}
话说回来,也就是我们的这个结构体压根就没有输出text值,只有erro和内存地址值
跟text有关的我们可以看到在text/template中
text/template
源码看到这里就豁然开朗了
结构体中第一个就是name的字符串
// Template is the representation of a parsed template. The *parse.Tree
// field is exported only for use by html/template and should be treated
// as unexported by all other clients.
type Template struct {name string*parse.Tree*commonleftDelim stringrightDelim string
}
所以,我们只需要修改我们的引用包位置就可以解决和例程输出不一致的问题
package mainimport ("fmt""text/template"// "html/template"
)func main() {// ******ParseFiles()方法*****t1 := template.New("test")t1, err := t1.ParseFiles("a.cnf", "b.cnf", "c.cnf")// ******ParseFiles()定义*****// t1, err := template.ParseFiles("a.cnf", "b.cnf", "c.cnf")if err != nil {panic(err)}// 先注释下面这行//t1.Parse("")fmt.Println(t1.DefinedTemplates())fmt.Println()fmt.Println(t1)fmt.Println(t1.Lookup("a.cnf"))fmt.Println(t1.Lookup("b.cnf"))fmt.Println(t1.Lookup("c.cnf"))
}
html/template 和 text/template区别相关推荐
- Text Template 模板
最近,使用到了 html/template 和 text/template 类库,通过使用模板,让项目清爽了不少.Go 的这个类似于和 PHP 的模板引擎,使用起来的话,类似于把大象关进冰箱的流程.第 ...
- text/template与html/template的区别
text/template 是将内容都已text文本格式返回. html/tempalte针对的是需要返回HTML内容的场景. 在模板渲染过程中会对一些有风险的内容进行转义,以此来防范跨站脚本攻击. ...
- T4 (Text Template Transformation Toolkit)实现简单实体代码生成
在很多场景下,我们都需要代码生成.你可以使用CodeSmith,不过它是商业软件.VisualStudio2008中自带也有代码生成功能.那就是T4 (Text Template Transforma ...
- <script type =“text / template”> ... </ script>的说明
本文翻译自:Explanation of I just stumbled upon something I've never seen before. 我偶然发现了一些我以前从未见过的东西. In t ...
- script type=text/template是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?...
这一段存放了一个模板. 在js里面,经常需要使用js往页面中插入html内容.比如这样: var number = 123; $('#d').append('<div class="t ...
- 对Strategy与Template Method模式的区别的一些讨论
准备抛弃自己原本在用的另一个blog了,开始把原本的一些文转过来. 这篇是[misc] 前两天与axx大聊天的一些记录. 转载开始: 10/02 2007, 星期二 主要是对Strategy与Temp ...
- Text Template Parser(多源数据提取软件)官方正式版V2.5 | 数据提取软件有哪些?
Text Template Parser 是一款简单实用的集数据检索.数据提取和数据转换的多数据源综合性文本数据提取软件,拥有友好的用户界面,可以帮助用户从包括文本文件.网页.电 ...
- text/template用法
1. 模板标签 模板标签用"{{"和"}}"括起来 2. 注释 # 使用"{{/*"和"*/}}"来包含注释内容 {{/ ...
- ITS Mobile Template interpretation failed. Template does not exist
ITS Mobile Template interpretation failed. Template does not exist 在SE80创建完ITS Mobile 应用后,SICF创建相同名称 ...
最新文章
- 一张时序图让你看懂:脏读、不可重复读
- 解决RedHat中ifconfig命令不能使用的问题
- Springmvc案例1----基于spring2.5的採用xml配置
- flex布局实现叠在另一个div之上_如何让一个div在另一个div的上面,求高手指点...
- React中的状态管理---Mobx
- BSP技术详解3---有图有真相
- springbot 注入多实例
- macOS开启内建的TFTP服务器
- 找出游戏的必胜的策略(博弈论的学习)
- 编译php时提示“Cannot find MySQL header files”的解决方法
- 用c语言编写24点游戏,C语言解24点游戏程序
- Doris支持spark导入设计文档
- 计算机辅助药物设计中的分子动力学模拟
- Docker Toolbox下配置国内镜像源-阿里云加速器
- 软件测试中期答辩,中期答辩材料创新张颖
- 深入原理64式:40 概率论公式总结
- 今日小程序推荐:纸塘-这个壁池真高清!
- 领英这样加人,一个月轻松加5000人
- 检查凭证录入模板的核算项目研发项目是否录入
- 2020春招补录全过程