Golang 模板引擎管道
Golang template engine pipelines
我有一个 Golang 模板,定义如下
{{- define "test" -}}
{{- printf "%s" .Name | trunc 24 -}}
{{- end -}}
然后我在我的一个文件中使用它:
{{ template "test" . }}
"test"后面的点是什么意思? Golang 模板文档说:
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
但我不确定管道是什么。看文档没有结果,谁能再解释一下?
此外,为什么我们必须以点开头的值开始?例如。 {{ - printf "%s" .Name | trunc 24 -}}
。是不是也是管道的一种?
提前致谢!
有 2 个 template
个包,text/template
and html/template
。
它们具有相同的接口,但是 html/template
包用于生成 HTML 安全输出以防止代码注入,并且应该在输出为 [=] 时代替 text/template
73=].
由于它们具有相同的界面,但 html/template
提供了一些额外的功能(插入数据的上下文转义),基础知识和原则仅记录在 text/html
,html/template
主要侧重于详细说明额外内容。
话虽这么说,"pipeline"属于基础知识。它记录在 text/template
部分 Pipelines:
Pipelines
A pipeline is a possibly chained sequence of "commands". A command is a simple value (argument) or a function or method call, possibly with multiple arguments:
Argument
The result is the value of evaluating the argument.
.Method [Argument...]
The method can be alone or the last element of a chain but,
unlike methods in the middle of a chain, it can take arguments.
The result is the value of calling the method with the
arguments:
dot.Method(Argument1, etc.)
functionName [Argument...]
The result is the value of calling the function associated
with the name:
function(Argument1, etc.)
Functions and function names are described below.
A pipeline may be "chained" by separating a sequence of commands with pipeline characters '|'. In a chained pipeline, the result of each command is passed as the last argument of the following command. The output of the final command in the pipeline is the value of the pipeline.
"Arguments"和"pipelines"是对数据的评价。
"dot" .
基本上是一个游标,指向执行模板时传递的数据结构中的某处。点的起始值就是你传递的值,但是这个点被很多动作修改了,比如{{range}}
或者{{with}}
.
Execution of the template walks the structure and sets the cursor, represented by a period '.' and called "dot", to the value at the current location in the structure as execution proceeds.
所以当你写.Name
时,这意味着点当前指向的值,你想引用它的字段或方法或键Name
。例如,如果你传递一个 struct
,在你的模板 .Name
的开头将表示结构字段 Name
如果它存在,或者它的方法名为 Name()
.
当您调用/包含另一个模板时,您可以告诉您将什么值传递给它的执行。当您写 {{template "something" .}}
时,这意味着您要将点当前指向的值传递给模板执行。如果你只想传递点指向的结构的 Name
字段,你可以这样做 {{template "something" .Name}}
.
您在 {{template}}
中作为管道传递的值将成为调用的其他模板中的点。
因此,在处理/呈现您的模板时,点可能会更改并指向 "only" 最初传递给您的模板执行的值的一部分。通常它很方便或需要仍然达到原始值而不仅仅是光标。为此,模板包提供了 $
:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
因此,即使您在 {{range}}
中(它将点设置为您正在测距的数组/切片/地图的连续元素),您仍然可以伸出手并参考传递给模板执行的值的任何其他部分。
因此,例如,如果您正在浏览像 {{range .Books}}
这样的书籍片段,并且如果您需要最初传递的结构的 Name
字段,您可以在 [=22] 中进行=] 像这样:
{{range .Books}}
Title: {{.Title}}
Original name: {{$.Name}}
{{end}}
我有一个 Golang 模板,定义如下
{{- define "test" -}}
{{- printf "%s" .Name | trunc 24 -}}
{{- end -}}
然后我在我的一个文件中使用它:
{{ template "test" . }}
"test"后面的点是什么意思? Golang 模板文档说:
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
但我不确定管道是什么。看文档没有结果,谁能再解释一下?
此外,为什么我们必须以点开头的值开始?例如。 {{ - printf "%s" .Name | trunc 24 -}}
。是不是也是管道的一种?
提前致谢!
有 2 个 template
个包,text/template
and html/template
。
它们具有相同的接口,但是 html/template
包用于生成 HTML 安全输出以防止代码注入,并且应该在输出为 [=] 时代替 text/template
73=].
由于它们具有相同的界面,但 html/template
提供了一些额外的功能(插入数据的上下文转义),基础知识和原则仅记录在 text/html
,html/template
主要侧重于详细说明额外内容。
话虽这么说,"pipeline"属于基础知识。它记录在 text/template
部分 Pipelines:
Pipelines
A pipeline is a possibly chained sequence of "commands". A command is a simple value (argument) or a function or method call, possibly with multiple arguments:
Argument The result is the value of evaluating the argument. .Method [Argument...] The method can be alone or the last element of a chain but, unlike methods in the middle of a chain, it can take arguments. The result is the value of calling the method with the arguments: dot.Method(Argument1, etc.) functionName [Argument...] The result is the value of calling the function associated with the name: function(Argument1, etc.) Functions and function names are described below.
A pipeline may be "chained" by separating a sequence of commands with pipeline characters '|'. In a chained pipeline, the result of each command is passed as the last argument of the following command. The output of the final command in the pipeline is the value of the pipeline.
"Arguments"和"pipelines"是对数据的评价。
"dot" .
基本上是一个游标,指向执行模板时传递的数据结构中的某处。点的起始值就是你传递的值,但是这个点被很多动作修改了,比如{{range}}
或者{{with}}
.
Execution of the template walks the structure and sets the cursor, represented by a period '.' and called "dot", to the value at the current location in the structure as execution proceeds.
所以当你写.Name
时,这意味着点当前指向的值,你想引用它的字段或方法或键Name
。例如,如果你传递一个 struct
,在你的模板 .Name
的开头将表示结构字段 Name
如果它存在,或者它的方法名为 Name()
.
当您调用/包含另一个模板时,您可以告诉您将什么值传递给它的执行。当您写 {{template "something" .}}
时,这意味着您要将点当前指向的值传递给模板执行。如果你只想传递点指向的结构的 Name
字段,你可以这样做 {{template "something" .Name}}
.
您在 {{template}}
中作为管道传递的值将成为调用的其他模板中的点。
因此,在处理/呈现您的模板时,点可能会更改并指向 "only" 最初传递给您的模板执行的值的一部分。通常它很方便或需要仍然达到原始值而不仅仅是光标。为此,模板包提供了 $
:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
因此,即使您在 {{range}}
中(它将点设置为您正在测距的数组/切片/地图的连续元素),您仍然可以伸出手并参考传递给模板执行的值的任何其他部分。
因此,例如,如果您正在浏览像 {{range .Books}}
这样的书籍片段,并且如果您需要最初传递的结构的 Name
字段,您可以在 [=22] 中进行=] 像这样:
{{range .Books}}
Title: {{.Title}}
Original name: {{$.Name}}
{{end}}