首页 > 其他 > 详细

golang 使用template funcmap的规则

时间:2014-03-12 06:03:38      阅读:570      评论:0      收藏:0      [点我收藏+]

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作为模板的名字就可以解决了。

例子

bubuko.com,布布扣
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)
    }
}
bubuko.com,布布扣

render_test.html

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

golang 使用template funcmap的规则,布布扣,bubuko.com

golang 使用template funcmap的规则

原文:http://www.cnblogs.com/ggaaooppeennngg/p/3594104.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!