golang multiplication algorithm in html template

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

golang multiplication algorithm in html template

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是golang的新手。在使用html/template时遇到了一个问题,当我使用乘法时出现了问题。
代码如下:

模板代码:

 {{range $i,$e:=.Items}}
      <tr>
           <td>{{add $i (mul .ID .Number)}}</td>
           <td>{{.Name}}</td>
      </tr>
  {{end}}

.go代码:

type Item struct{
        ID int
        Name string
    }
func init() {
    itemtpl,_:=template.New("item.gtpl").
		Funcs(template.FuncMap{"mul": Mul, "add": Add}).
		ParseFiles("./templates/item.gtpl")
}

func itemHandle(w http.ResponseWriter, req *http.Request) {

    items:=[]Item{Item{1,"name1"},Item{2,"name2"}}
    data := struct {
			Items []Item
            Number int
            Number2 int
		}{
			Items:    items,
			Number:   5,
            Number2:  2,
		}
		itemtpl.Execute(w, data)
}
func Mul(param1 int, param2 int) int {
	return param1 * param2
}
func Add(param1 int, param2 int) int {
	return param1 + param2
}

当我使用上述代码时,它不会输出任何内容。但是,当我在数组之外使用以下代码时,它会输出10。

<html>
<body>
    {{mul .Number .Number2}}
</html>
</body>

我在Google上搜索了很多,但找不到类似我的可用解决方案。我想在html/template中的数组中使用乘法。有人可以告诉我我的代码有什么问题吗?

英文:

I am new for golang. I met a problem when I use multiplication in html/template.
Some code like below.

template code:

 {{range $i,$e:=.Items}}
      <tr>
           <td>{{add $i (mul .ID .Number)}}</td>
           <td>{{.Name}}</td>
      </tr>
  {{end}}

.go code

type Item struct{
        ID int
        Name string
    }
func init() {
    itemtpl,_:=template.New("item.gtpl").
		Funcs(template.FuncMap{"mul": Mul, "add": Add}).
		ParseFiles("./templates/item.gtpl")
}

func itemHandle(w http.ResponseWriter, req *http.Request) {

    items:=[]Item{Item{1,"name1"},Item{2,"name2"}}
    data := struct {
			Items []Item
            Number int
            Number2 int
		}{
			Items:    items,
			Number:   5,
            Number2:  2,
		}
		itemtpl.Execute(w, data)
}
func Mul(param1 int, param2 int) int {
	return param1 * param2
}
func Add(param1 int, param2 int) int {
	return param1 + param2
}

It will output nothing when I use the code above. But It will output 10 when I use the code outside of array below.

<html>
<body>
    {{mul .Number .Number2}}
</html>
</body>

I google a lot. I cannot find the usable like mine. I want to use multiplication in array inside of html/template. Can someone tell me what is wrong with my code?

答案1

得分: 3

template.Execute() 返回一个 error,你应该始终检查它。你是否已经这样做了:

> template: item.gtpl:3:33: 在<.Number>处执行"item.gtpl"时出错:Number不是main.Item结构类型的字段

问题在于 {{range}} 改变了管道(即 dot.)到当前的项,所以在 {{range}} 内部:

{{add $i (mul .ID .Number)}}

.Number 将引用你的 Item 类型的字段或方法,因为你正在遍历一个 []Item。但是你的 Item 类型没有这样的方法或字段。

使用 $.Number,它将引用"顶级"的 Number 而不是当前 Item 值的字段:

{{add $i (mul .ID $.Number)}}

Go Playground 上尝试你修改后的可工作代码。

$text/template 中有文档说明:

> 当执行开始时,$ 被设置为传递给 Execute 的数据参数,也就是 dot 的起始值。

英文:

template.Execute() returns an error, you should always check that. Would you have done so:

> template: item.gtpl:3:33: executing "item.gtpl" at <.Number>: Number is not a field of struct type main.Item

The "problem" is that {{range}} changes the pipeline (the dot, .) to the current item, so inside the {{range}}:

{{add $i (mul .ID .Number)}}

.Number will refer to a field or method of your Item type since you are looping over a []Item. But your Item type has no such method or field.

Use $.Number which will refer to the "top-level" Number and not a field of the current Item value:

{{add $i (mul .ID $.Number)}}

Try your modified, working code on the Go Playground.

$ is documented at text/template:

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

huangapple
  • 本文由 发表于 2016年2月1日 17:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35127051.html
匿名

发表评论

匿名网友

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

确定