V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
46fo

关于go的模板问题

  •  
  •   46fo · Nov 26, 2013 · 1670 views
    This topic created in 4537 days ago, the information mentioned may be changed or developed.
    h.html
    <p>{{.H}}</p>


    c.html
    {{template "h.html"}}
    <p>{{.C}}</p>
    {{template "f.html"}}


    f.html
    <p>{{.F}}</p>


    type data_t struct {
    H string
    C string
    F string
    }


    t := template.ParseFiles("h.html", "c.html", "f.html") //加载三个

    index := data_t{H: "头", C: "内容", F: "尾"}

    t.ExecuteTemplate(rw, "c.html", index)
    为什么只替换了C , H和F没替换, c.html不是包含了 f.html和h.html吗?按理说也会自动替换啊。




    把c.html改成
    c.html
    <p>{{.C}}</p>

    然后
    t.ExecuteTemplate(rw, "h.html", index)
    t.ExecuteTemplate(rw, "c.html", index)
    t.ExecuteTemplate(rw, "f.html", index)
    这样连着就可以, 这样未免也太挫了, 那上面的包含还有意义吗? 请教有没有更好的办法。。。
    6 replies    1970-01-01 08:00:00 +08:00
    46fo
        1
    46fo  
    OP
       Nov 26, 2013
    @wwwjfy
    @ jun0205 能帮忙么
    46fo
        2
    46fo  
    OP
       Nov 26, 2013
    @jun0205 能帮忙么
    gihnius
        3
    gihnius  
       Nov 26, 2013   ❤️ 1
    c.html
    {{template "h.html"}}
    <p>{{.C}}</p>
    {{template "f.html"}}

    改成

    c.html
    {{template "h.html" .}}
    <p>{{.C}}</p>
    {{template "f.html" .}}

    才能将数据传过去
    46fo
        4
    46fo  
    OP
       Nov 26, 2013
    @gihnius 非常感谢! 还真是这样。。。 我找的go资料很少
    46fo
        5
    46fo  
    OP
       Nov 26, 2013
    @gihnius 你能有 好的 全面一点的 资料分享一下么! 官方的太不全了
    sillyousu
        6
    sillyousu  
       Nov 30, 2013
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1344 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 17:18 · PVG 01:18 · LAX 10:18 · JFK 13:18
    ♥ Do have faith in what you're doing.