Golang 模板引擎管道 [英] Golang template engine pipelines
问题描述
我有一个 Golang 模板,定义如下
I have a Golang template, defined like this
{{- define "test" -}}
{{- printf "%s" .Name | trunc 24 -}}
{{- end -}}
然后我在我的一个文件中使用它:
Then I use it in one of my files:
{{ template "test" . }}
测试"后的点是什么意思?Golang 模板文档说:
What does the dot mean after "test"? Golang template docs say:
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
但我不确定管道是什么.看文档没有结果,谁能再解释一下?
But I am not sure what pipeline is. Reading documentation gave no results, could anyone explain once again?
另外,为什么我们必须以点开头的值?例如.{{ - printf "%s" .Name |截断 24 -}}
.也是一种管道吗?
Also, why do we have to start values beginning with dot? E.g. {{ - printf "%s" .Name | trunc 24 -}}
. Is it also a kind of pipeline?
先谢谢你!
推荐答案
有 2 个 template
包,text/template
和 html/模板
.
There are 2 template
packages, text/template
and html/template
.
它们具有相同的界面,但是 html/template
包用于生成安全的 HTML 输出以防止代码注入,并且应该在出现以下情况时使用 text/template
而不是输出是 HTML.
They have the same interface, but the html/template
package is for generating HTML output safe against code injection, and should be used instead of text/template
whenever the output is HTML.
由于它们具有相同的界面,但 html/template
提供了一些额外的功能(插入数据的上下文转义),基础知识和原则仅记录在 text/html
,html/template
的文档主要集中在细节上.
Since they have the same interface but the html/template
provides some extra functionality (contextual escaping of the inserted data), the basics and principles are only documented at text/html
, and the documentation of html/template
mostly focuses on detailing the extra.
话虽如此,管道"属于基础知识.它记录在 text/template
, section Pipelines:
That being said, "pipeline" belongs to the basics. It is documented in text/template
, section 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" and "pipelines" are evaluations of data.
点".
基本上是一个游标,指向执行模板时传递的数据结构中的某处.点的起始值是你传递的值,但是这个点被很多动作修改,比如{{range}}
或{{with}}
.
The "dot" .
is basically a cursor, pointing to somewhere in the data structure you pass when executing the template. The starting value of the dot is the value you pass, but this dot is modified by many actions, such as {{range}}
or {{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()
.
So when you write .Name
, that means that the value where dot is pointing currently, you want to refer to its field or method or key called Name
. For example if you pass a struct
, at the beginning of your template .Name
will denote the struct field Name
if it exists, or its method named Name()
.
当你调用/包含另一个模板时,你有可能告诉你要传递给它执行的什么值.当您编写 {{template "something" .}}
时,这意味着您希望将当前由点指向的值传递给模板执行.如果你只想传递点所指向的结构体的 Name
字段,你可以像 {{template "something" .Name}}
那样做.
When you invoke / include another template, you have the possibility to tell what value you what to pass to its execution. When you write {{template "something" .}}
, that means you want to pass the value currently pointed by dot to the template execution. If you want to pass only the Name
field of the struct pointed by the dot, you may do it like {{template "something" .Name}}
.
您在 {{template}}
中作为管道传递的值将成为调用的其他模板中的点.
The value you pass as the pipeline in {{template}}
will become the dot inside the invoked other template.
因此,当您的模板正在处理/呈现时,点可能会更改并仅"指向最初传递给您的模板执行的值的一部分.通常它很方便或需要仍然达到原始值而不仅仅是光标.为此,模板包提供了 $
:
So as your template is being processed / rendered, the dot may be changed and point "only" to a part of the value originally passed to your template execution. Often it's handy or required to still reach the original value and not just the cursor. For this the template package provides the $
:
执行开始时,$设置为传递给Execute的数据参数,即dot的起始值.
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
因此,即使您在 {{range}}
中,例如(它将点设置为您正在浏览的数组/切片/地图的连续元素),您也可以仍然伸出并引用传递给模板执行的值的任何其他部分.
So even if you're inside a {{range}}
for example (which sets the dot to the successive elements of the array / slice / map you're ranging over), you can still reach out and refer to any other parts of the value passed to the template execution.
因此,例如,如果您正在浏览诸如 {{range .Books}}
之类的书,并且如果您需要原始传递的 Name
字段struct,你可以在 {{range}}
中这样做:
So for example if you're ranging over a slice of books like {{range .Books}}
, and if you need the Name
field of the originally passed struct, you may do it inside {{range}}
like this:
{{range .Books}}
Title: {{.Title}}
Original name: {{$.Name}}
{{end}}
这篇关于Golang 模板引擎管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!