在text/html模板包中,“range”操作和“pipeline”概念的解释。Golang

huangapple go评论81阅读模式
英文:

"range" action and "pipeline" clarification in text/html template package. Golang

问题

我尝试理解text/html模板包中的一些要点。我已经阅读了来自golang网站的文档。很难理解在一般情况下以及在range操作中的点号(.)的确切含义。"pipeline"到底是什么意思,也许很难理解是因为我的英语不是母语。

{{pipeline}}
将管道的值的默认文本表示复制到输出。

让我们来看一个例子:

data := map[string]interface{}{
"struct": &Order{
ID: 1,
CustID: 2,
Total: 3.65,
Name: "Something",
},
"name1": "Timur",
"name2": "Renat",
}
t.ExecuteTemplate(rw, "index", data)

这是 "index":

{{define "index"}}
{{range $x := .}}
{{.}}
{{$x}}
{{$.struct.ID}}

# 下面的行不起作用,会中断循环
# {{.ID}}
# 或者
# {{.struct.ID}}

    # 如果我想在这里使用另一个range循环来处理 "struct" 成员
    # 当我在数据变量中到达 "struct" 字段时,或者什么都不做
    # 只是继续循环?

{{end}}

{{end}}

输出:

Timur
Timur
1

Renat
Renat
1

{1 2 3.65 Something}
{1 2 3.65 Something}
1

英文:

I try to get some fine points in text/html template package. I've read its documentation from golang site. It's difficult to understand what exactly means the . (dot) in general and at a certain time in the range action. What exactly means "pipeline", maybe it's difficult to understand because my English is not mother tongue):

{{pipeline}}
The default textual representation of the value of the pipeline
is copied to the output.

Let's consider an example:

	data := map[string]interface{}{
		"struct": &Order{
			ID:     1,
			CustID: 2,
			Total:  3.65,
			Name: "Something",
		},
		"name1":  "Timur",
		"name2": "Renat",
	}
	t.ExecuteTemplate(rw, "index", data)

Here is "index":

{{define "index"}}
    {{range $x := .}}
        {{.}}
        <b>{{$x}}</b><br>
        <i>{{$.struct.ID}}</i><br>
        <br>
        # the lines below don't work and break the loop
        # {{.ID}}
        # or
        # {{.struct.ID}}

        # what if I want here another range loop that handles "struct" members
        # when I reach "struct" field in the data variable or just do nothing
        # and just continue the loop? 
    {{end}}
{{end}}

Output:

Timur<br>
Timur<br>
1<br>

Renat<br>
Renat<br>
1<br>

{1 2 3.65 Something}<br>
{1 2 3.65 Something}<br>
1<br>

答案1

得分: 7

管道

模板包中的管道指的是在命令行中使用的“管道”操作。

例如,以下是在Mac上获取默认网关的方法之一:

route -n get default | grep 'gateway' | awk '{print $2}';

基本上,首先运行route -n get default命令。而不是将结果打印到控制台,管道字符|表示“将route命令的输出传递给grep命令”。此时,grep 'gateway'在接收到来自route的输入后运行。grep的输出然后传递给awk。最后,由于没有更多的管道,你在屏幕上看到的唯一输出就是awk要打印的内容。

在模板包中也是类似的。你可以将值传递给方法调用并将它们链接在一起。例如:

{{ "Hello world!" | printf "%s" }}

这等同于 {{ printf "%s" "Hello World!" }}

<kbd>在Go Playground中查看示例</kbd>

基本上,

{{ "Hello World!" | printf "%s"           }}
    ^^^^^^^^^^^^               ^^^^^^^^^^
         |__________________________|

这在函数式语言中非常常见(根据我所见...我知道在F#中是这样的)。

点号 .

点号是“上下文感知”的。这意味着它的含义取决于它所在的位置。当你在模板的正常区域使用它时,它表示模型。当你在range循环中使用它时,它表示当前迭代的值。

<kbd>在Go Playground中查看示例</kbd>

在链接的示例中,只有在range循环内部,$x.是相同的。一旦循环结束,点号将指回传递给模板的模型。

检查“struct”

你的结构体是一个键值对...一个map。因此,你需要确保在range循环中提取两个部分:

{{ range $key, $value = . }}

这将在每次迭代中给你键和值。之后,你只需要检查相等性:

{{ if eq $key "struct" }}
    {{ /* $value.ID 是你想要的ID */ }}

<kbd>在Go Playground中查看示例</kbd>

希望能对你有所帮助。

英文:

Pipelines

A pipeline in the template package refers to the same sort of "piping" you would do at the command line.

For example, this is one way to get the default gateway assigned to your NIC on a Mac:

route -n get default | grep &#39;gateway&#39; | awk &#39;{print $2}&#39;

Basically, the route -n get default is run first. Instead of printing the result to the console, the pipe character | says "take the output of the route command, and shove it into the grep command". At this point, grep &#39;gateway&#39; runs on the input it receives from route. The output of grep is then shoved into awk. Finally, since there are no more pipes, the only output that you see on screen is whatever awk wants to print.

This is sort of the same in the template package. You can pipe values into method calls and chain them together. Such as:

{{ &quot;Hello world!&quot; | printf &quot;%s&quot; }}

Which is the equivalent of {{ printf &quot;%s&quot; &quot;Hello World!&quot; }}

<kbd>See an example in the Go Playground here</kbd>

Basically,

{{ &quot;Hello World!&quot; | printf &quot;%s&quot;           }}
    ^^^^^^^^^^^^               ^^^^^^^^^^
         |__________________________|

This is a very common thing in functional languages (from what I've seen.. I know its a thing in F#).

The dot .

The dot is "contextually aware". What this means, is that it changes meaning depending on where you put it. When you use it in the normal areas of your template, it is your model. When you use it in a range loop, it becomes the current value of the iteration.

<kbd>See an example in the Go Playground here</kbd>

In the linked example, only within the range loop, $x and . are the same. Once the loop ends, the dot refers back to the model passed into the template.

Checking for "struct"

Your struct is a key-value pair ... a map. To that end, you need to make sure you extract both parts in your range loop:

{{ range $key, $value = . }}

This will give you both the key and the value at each iteration. After that, you just need to check equality:

{{ if eq $key &quot;struct&quot; }}
    {{ /* $value.ID is the ID you want */ }}

<kbd>See an example on the Go Playground here</kbd>

Hopefully that helps.

huangapple
  • 本文由 发表于 2015年1月16日 05:03:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/27972939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定