html/template 模板range输出问题

想在模板中, 类似如下: {{range .val }} {{.}} {{ .othervar }} {{ end}} 中输出其它变量othervar,一直没有成功,不知道怎么办, 请教一下该怎么办, 详细代码如下:

package main
import(
        "html/template"
        "os"
)

type Hello struct{
        Name  string
        Age   int
        Email  string
}

func main(){
        var str string = "hello world"
        var hello  []Hello
        hello = append(hello, Hello{Name:"test",Age:35,Email:"qq@qq.com"})
        hello = append(hello, Hello{Name:"test2",Age:12,Email:"lwei@qq.com"})
        var tmp map[string]interface{}
        tmp = make(map[string]interface{})

        t := template.New("hello.html")
        tmp["info"] = hello
        tmp["Strinfo"] = str
        t, _ = t.ParseFiles("hello.html")
        t.Execute(os.Stdout, tmp)
}

hello.html:

<html>
        <head>
                <title>Test page</title>
        </head>
        <body>
             {{range .info }}
             <span>  Name is {{ .Name }} </span>
             <span>  Age is {{ .Age }} </span>
             <span>  Email is {{ .Email }} </span>
             <span> {{ .Strinfo }} </span>
             {{ end }}

        </body>
</html>
已邀请:

golwei

赞同来自:

<html>
        <head>
                <title>Test page</title>
        </head>
        <body>
             {{range .info }}
             <span>  Name is {{ .Name }} </span>
             <span>  Age is {{ .Age }} </span>
             <span>  Email is {{ .Email }} </span>
             {{ end }}
             <span> {{ .Strinfo }} </span>
        </body>
</html>

Yue

赞同来自:

{{ $.Strinfo }}

kggg

赞同来自:

Yue, 谢谢, 这个可以了。 $ 符号代表什么?

要回复问题请先登录注册