万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    Ginでテンプレートを入れ子

    Help with nested template · Issue #339 · gin-gonic/gin · GitHub ここを見た。

    go get github.com/gin-gonic/contrib/renders/multitemplate してから、

    import (
            "github.com/gin-gonic/contrib/renders/multitemplate"
    )

    結局。親から子を指定しないといけない? 本当は子だけで完結したいのだが・・・・。

            templates := multitemplate.New()
            templates.AddFromFiles("contact",
                    "templates/tra/Base.html",
                    "templates/tra/Navbar.html",
                    "templates/tra/Contact.html")

    contactという名前で各テンプレートを集約?

    そしてcontactc.HTML(200, "contact", gin.H{のとこで指定してる。 ここでshops/index.tmplのようにいつもしてたようなところが、contactになるっぽい。

            controller.Router.HTMLRender = templates
            controller.Router.GET("/contact", func(c *gin.Context) {
                    c.HTML(200, "contact", gin.H{
                    "title": "Contact",
                    "stuff": "Interesting contact stuff",
                    })
            })

    templates/tra/

    traってのはテキトーなので気にせず。

    templates/tra/Base.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>{{.title}}</title>
    </head>
    <body>
    {{ .hoge }}
    </body>
    </html>

    templates/tra/Navbar.html

    {{define "navbar"}}
    <div class="nav">This is a navbar</div>
    {{end}}

    templates/tra/Contact.html

    {{define "contact"}}
    <div>
        Contact content
        {{.stuff}}
    </div>
    {{end}}

    しかし、親、つまり「templates/tra/Base.html」に子のtemplateの指定を書かないといけないのは不便だ。 子側から親を指定した方が便利だと思うけど、子が変わるのだがどうやるべき??