Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i pass a variable to all templates #2622

Closed
3 tasks done
nickfthedev opened this issue Sep 5, 2023 · 3 comments
Closed
3 tasks done

How can i pass a variable to all templates #2622

nickfthedev opened this issue Sep 5, 2023 · 3 comments

Comments

@nickfthedev
Copy link

Question Description

Is it possible to pass for example a variable across all templates?

For example the user data from a middlware to all templates?

Code Snippet (optional)

// Main.go

        page := app.Group("/", middleware.IsLoggedIn) 
         htmx := app.Group("/") 
         // 
         // Routes which render sites 
         // 
         page.Get("/", func(c *fiber.Ctx) error { 
                 fmt.Println(c.Locals("id")) 
                 // Render index template 
                 return c.Render("index", fiber.Map{ 
                         "Title": "Hello, World!", 
                 }) 
         })

// middleware 

func IsLoggedIn(c *fiber.Ctx) error { 
         user, _ := CheckLogin(c) 
         if &user != nil {
// THIS IS REQUIRED IN ALL TEMPLATES
// PART OF THE MAIN MENU IN LAYOUT 
                 c.Locals("name", user.Name) 
                 c.Locals("id", user.ID) 
                 c.Locals("isLoggedIn", true) 
         } 

         c.Locals("message", "Hello, World!") 
         return c.Next() 
 }

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@welcome
Copy link

welcome bot commented Sep 5, 2023

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

https://docs.gofiber.io/api/fiber#config

Try the config setting PassLocalsToViews

@nickfthedev
Copy link
Author

Thank you so much, I was looking 3 days for a solution to my problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants