golang 使用template funcmap的规则

http://stackoverflow.com/questions/10199219/go-template-function

http://stackoverflow.com/questions/15209236/multiple-templates-with-funcmap

https://groups.google.com/forum/#!topic/golang-nuts/0Q-r_EkCPgk

总之解决方法就是,以首个文件的basename作为模板的名字就可以解决了。

例子

func TestFuncMap(T *testing.T){
    //os.Chdir("controller")
    t:=template.New("render_test.html")
    t.Funcs(funcMap)
    t,err:=t.ParseFiles("render_test.html")
    if err!=nil{
        panic(err)
    }
    render:=make(map[string]interface{})
    render["homeworkid"]=bson.NewObjectId()
    render["name"]="title man"
    if err =t.Execute(os.Stdout,render);err!=nil{
        panic(err)
    }
}

render_test.html

{{.name}}
{{.homeworkid}}
{{hex .homeworkid}}

 

posted @ 2014-03-11 15:44  ggaaooppeenngg  阅读(2198)  评论(0编辑  收藏  举报