golang模板引擎管道 [英] golang template engine pipelines
问题描述
我有一个golang模板,定义如下:
{{ - definetest - }}
{{ - printf%s.Name | trunc 24 - }}
{{ - end - }}
然后我用它我的一个档案:
{{templatetest。 }}
在测试之后,点意味着什么? Golang模板文档说:
{{templatenamepipeline}}
具有指定名称的模板被执行点设置
到管道的值。
但我不确定管线是什么。阅读文档没有给出任何结果,任何人都可以再解释一次吗?
另外,为什么我们必须以点开始值呢?例如。 {{ - printf%s.Name | trunc 24 - }}
。它是一种管道吗?
提前感谢您!
有2个 它们有相同的接口,但是 由于它们具有相同的接口,但 这就是说,管道属于基本知识。它记录在 流水线一个可能链接的命令序列。命令是一个简单的值(参数)或函数或方法调用,可能带有多个参数: 可以通过用流水线字符'|'分隔一系列命令来连接管道。在链式管道中,每个命令的结果都作为以下命令的最后一个参数传递。管道中的最终命令的输出是管道的值。 参数和管道是对数据的评估。 dot 模板的执行遍历结构并设置游标, 。'并且叫做dot,作为执行过程中当前位置的值。 所以当你写 当你调用/包含另一个模板时,你有可能告诉你传递给它的执行什么值。当您编写模板
包, text / template
和 <$ c
$ b html / template
package用于生成HTML代码注入的安全输出,每当输出为HTML时,应该使用它来代替 text / template
。
html / template
提供了一些额外的功能(插入数据的上下文转义)并且原理仅记录在 text / html
中,并且 html / template
的文档主要集中在详细说明额外内容。
text / template
中, Pipelines :
$ b
流水线
参数
结果是评估论证的价值。
.Method [参数...]
该方法可以是单独的,也可以是链中的最后一个元素,但与链中间的方法不同,
可能需要参数。
结果是使用
参数调用方法的值:
dot.Method(Argument1等)
functionName [参数...]
结果是调用与名称
关联的函数的值:
函数(参数1等)
函数和函数名称如下所述。
$ b 。
基本上是一个游标,指向执行时通过的数据结构中的某处模板。点的起始值是您传递的值,但此点由许多操作修改,如 {{range}}
或 {{使用}}
。
.Name
,这意味着当前点正在指向的值,您要引用其名称为名称$ c $的字段或方法或键C>。例如,如果你传递了一个
struct
,那么在你的模板 .Name
的开头将表示结构字段名称
如果它存在,或者它的方法名为 Name()
。
{{templatesomething。}}
时,这意味着您要将当前指向的点值传递给模板执行。如果只想传递点所指向的结构体的 Name
字段,可以像 {{templatesomething.Name}您在
。 {{template}}中传递的值
因此,当您的模板正在被处理/渲染时,点可能会改变并指向仅值最初传递给您的模板执行。通常它很方便或需要达到原始值,而不仅仅是光标。为此,模板包提供了 $
:
$ b
执行开始时,$是设置为传递给Execute的数据参数,也就是点的起始值。
所以即使你在一个例如(它将点设置为数组/片/地图的连续元素,你可以覆盖),你仍然可以伸出手并引用到传递给模板执行的值的任何其他部分。
例如,如果您正在阅读一些书籍,如 {{如果你需要最初传递结构的
Name
字段,你可以在 {{range}}
像这样:
{{range .Books}}
标题:{{.Title}}
原始名称:{{$ .Name}}
{{end}}
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" . }}
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?
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?
Thank you in advance!
There are 2 template
packages, text/template
and html/template
.
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.
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.
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.
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.
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()
.
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}}
.
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 $
:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
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.
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屋!