解析模板文件并绑定方法和数据
|
|
将data传入“tpl/index.gohtml”模板进行解析,将解析后的数据传入到writer,writer可以是http请求的responseWriter,也可以是file(file实现了writer接口)。 也就是说,可以作为响应数据返回到浏览器前端,也可以保存到文件中。
template的Execute方法的文档:
Execute 将已解析的模板应用于指定的数据对象,并将输出写入 wr。
如果执行模板或写入其输出时发生错误,执行将停止,但部分结果可能已写入输出写入器。
模板可以安全地并行执行,但如果并行执行共享一个 Writer,则输出可能是交错的。
定义模板
|
|
使用模板
|
|
使用数据
举几个例子
|
|
{{$Users := .Users}}
{{with pipeline}} T1 {{end}}
If the value of the pipeline is empty, no output is generated;
otherwise, dot is set to the value of the pipeline and T1 is
executed.
{{with pipeline}} T1 {{else}} T0 {{end}}
If the value of the pipeline is empty, dot is unaffected and T0
is executed; otherwise, dot is set to the value of the pipeline
and T1 is executed.
pipeline 指的数据