templates - Golang 模板引擎管道

标签 templates go go-templates

我有一个 Golang 模板,定义如下

{{- define "test" -}}
{{- printf "%s" .Name | trunc 24 -}}
{{- end -}}

然后我在我的一个文件中使用它:

{{ template "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 |截断 24 -}}。是不是也是一种管道?

提前致谢!

最佳答案

有 2 个 template 包,text/templatehtml/template .

它们具有相同的界面,但是 html/template 包用于生成 HTML 输出以防止代码注入(inject),并且应该在任何时候使用而不是 text/template输出为 HTML。

由于它们具有相同的界面,但 html/template 提供了一些额外的功能(插入数据的上下文转义),因此基础知识和原则仅记录在 text/html,而 html/template 的文档主要侧重于详细说明额外内容。

话虽这么说,“流水线”属于基础。它记录在 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.

“参数”和“管道”是对数据的评估。

“点”. 基本上是一个游标,指向您在执行模板时传递的数据结构中的某处。点的起始值是你传递的值,但是这个点被很多 Action 修改,比如{{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}} 中作为管道传递的值将成为调用的其他模板内的点。

因此,在处理/呈现您的模板时,点可能会更改并“仅”指向最初传递给您的模板执行的值的一部分。通常它很方便或需要仍然达到原始值而不仅仅是光标。为此,模板包提供了 $:

When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.

因此,即使您在 {{range}} 中(它将点设置为您正在测距的数组/slice/ map 的连续元素),您也可以仍然伸出手来引用传递给模板执行的值的任何其他部分。

因此,例如,如果您正在遍历像 {{range .Books}} 这样的书籍片段,并且如果您需要最初传递的 Name 字段struct,你可以在 {{range}} 中这样做:

{{range .Books}}
    Title: {{.Title}}
    Original name: {{$.Name}}
{{end}}

关于templates - Golang 模板引擎管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42507958/

相关文章:

java - 来自 http 调用和表单的 Meteor 动态模板

c++ - 如何创建模板 vector ?

go - 如何从 Go channel 获取(和忽略)一个值

c++ - 模板的 C++ 部分特化错误

c# - 无法创建变量类型 'Item' 的实例,因为它没有 new() 约束

go - 弃用和收回之间的区别?

go - 如何使用 Go 串行库检查线路状态(RS232)

go - 如何将多个变量传递给 Go HTML 模板

go - 直接转到 Golang 中间件内的新页面

go - 有没有一种有效的方法来连接字符串