Gin: How can I define template in separate HandlerFunc

Created on 23 Aug 2018  路  5Comments  路  Source: gin-gonic/gin

I moved HandlerFunc to a separate file, but I need to define the specific template for this action, so I have

main.go

func main() {
    router := gin.Default()
    router.Static("/assets", "./assets")
    router.LoadHTMLGlob("templates/*/**")
    routes.Attach(router)

    router.Run(":9000")
}

router.go

func Attach(router *gin.Engine) {
    router.GET("/", controllers.Root)
}

controllers.go

func Root(c *gin.Context) {
    // how can I define specific template here???
    // router.SetHTMLTemplate(template.Must(template.ParseFiles("templates/layouts/main.tmpl", "templates/root/root.tmpl")))
    c.HTML(200, "base", gin.H{
        "title": "Root page",
    })
}

so is it possible to specify template in the separate function?

question

All 5 comments

@olegsobchuk May be grouping-routes can help you.

@chainhelen I want to use the same layout but different content template for every action
so one action has
router.SetHTMLTemplate(template.Must(template.ParseFiles("templates/layouts/main.tmpl", "templates/root/root.tmpl")))
another one
router.SetHTMLTemplate(template.Must(template.ParseFiles("templates/layouts/main.tmpl", "templates/users/index.tmpl"))) another
router.SetHTMLTemplate(template.Must(template.ParseFiles("templates/layouts/main.tmpl", "templates/users/show.tmpl")))
and so on

@olegsobchuk Ok, I get it.Maybe gin-contrib/multitemplate can help you. Here is one example.

@chainhelen thanks seems like it works
I just leave sample here, maybe it will be useful for somebody else
main.go

package main

import (
    "github.com/gin-contrib/multitemplate"
    "github.com/gin-gonic/gin"
    "github.com/olegsobchuk/go-gin-app/routes"
)

func main() {
    router := gin.Default()
    router.Static("/assets", "./assets")
    router.LoadHTMLGlob("templates/*/**")
    router.HTMLRender = buildTemplate()
    routes.Attach(router)

    router.Run(":9000")
}

func buildTemplate() multitemplate.Renderer {
    r := multitemplate.NewRenderer()
    r.AddFromFiles("base", "templates/layouts/main.tmpl", "templates/root/root.tmpl")
    r.AddFromFiles("login", "templates/layouts/main.tmpl", "templates/session/login.tmpl")
    return r
}

router.go

func Attach(router *gin.Engine) {
    router.GET("/", controllers.Root)
    router.GET("/login", controllers.LogIn)
}

controllers.go

// Root root page
func Root(c *gin.Context) {
    c.HTML(200, "base", gin.H{
        "title": "Root page",
    })
}

// LogIn login user page
func LogIn(c *gin.Context) {
    c.HTML(200, "login", gin.H{
        "title": "LogIn page",
    })
}

layout template

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Fun4Fan - {{ .title }}</title>
    <style type="text/css" media="screen">
      @import url("/assets/css/main.css");
    </style>
  </head>
  <body>
    I'm layout
    {{template "content" .}}
  </body>
</html>

Root template

{{define "content"}}
  <div class="">
    <a href="/login">Log In</a>
  </div>
  <div class="container">
    I'm Root page
    Root page
  </div>
{{end}}

login template
.....

As for me issue may be closed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iiinsomnia picture iiinsomnia  路  3Comments

rawoke083 picture rawoke083  路  3Comments

frederikhors picture frederikhors  路  3Comments

cxk280 picture cxk280  路  3Comments

wangcn picture wangcn  路  3Comments