You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have below route my project with name now i wanna call them inside templates
func ProductRoutes(a *fiber.App) {
pc := controllers.ProductController{}
// admin route
route := a.Group("/admin/product")
// add auth middleware to dashboard group
route.Use(middleware.AuthProtected)
route.Get("/", pc.Index).Name("product_index") // get all products
route.Get("/add", pc.Add).Name("product_add") // add new product form
route.Post("/", pc.Insert).Name("product_insert") // add new product to database
route.Get("/:id", pc.Show).Name("product_show") // get a product with an ID
route.Put("/:id", pc.Update).Name("product_update") // update a product with an ID
route.Delete("/:id", pc.Destroy).Name("product_delete") // delete a product
}
You can get route's URL with c.GetRouteURL(), however, *fiber.Ctx is not available in the template. I have gone the same path as you, and passed the *fiber.Ctx to template with a middleware:
Then I have a middleware that passes *fiber.Ctx to template via locals.
func New(c *fiber.Ctx) error {
c.Locals("C", c)
return c.Next()
}
With this in place, you can use {{.C.GetRouteURL "template_name" nil}} in your template.
If your routes has parameters in them, you will need to create a template function that will enable you to create parameters map. I have something like this:
template.FuncMap{
"map": func(args ...any) (fiber.Map, error) {
if len(args)%2 != 0 {
return nil, errors.New("number of params should be even")
}
m := fiber.Map{}
for i := 0; i < len(args); i += 2 {
m[args[i].(string)] = args[i+1]
}
return m, nil
},
}
I have below route my project with name now i wanna call them inside templates
Below my controller
I wanna call
product_index
inside templateI tried few ways and search over google didn't found any answer, is there any way to do that?
The text was updated successfully, but these errors were encountered: