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

Context pre-processors #18

Closed
wsantos opened this issue Aug 16, 2020 · 4 comments
Closed

Context pre-processors #18

wsantos opened this issue Aug 16, 2020 · 4 comments

Comments

@wsantos
Copy link
Contributor

wsantos commented Aug 16, 2020

Hi I need to extract data from context and expose it to the template, mainly sure info so I can show that the user is logged in on the page header, do we have a way to do it ? do you think that maybe we can have the same functionality as Config.Funcs but they receive context? here it's my test middleware and the data I need available on all templates.

// UserAuth -
func (m *Middleware) UserAuth(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		sess, _ := session.Get("session", c)
		userId, _ := sess.Values["user_id"].(int)

		user, _ := models.FindUser(c.Request().Context(), m.DB, userId)

		c.Set("user", user)

		return next(c)
	}
}

And for echoview I think that this needs to go here, echoview.go

// Render render template for echo interface
func (e *ViewEngine) Render(w io.Writer, name string, data interface{}, c echo.Context) error {

    // call all context processors and transform data

	return e.RenderWriter(w, name, data)
}

Let me know if that is a good approach, but I'm not sure how to fit this on other frameworks.

@foolin
Copy link
Owner

foolin commented Aug 16, 2020

Wrapper utils function for this data:

e.GET("/page", func(c echo.Context) error {

		return c.Render(http.StatusOK, "page.html", AuthData(c, echo.Map{"title": "Page file title!!"}))
})

func AuthData(c echo.Context, echo.Map) echo.Map{

  user := c.Get("user")
    //merge data with data
   return xxxx
}

@wsantos
Copy link
Contributor Author

wsantos commented Aug 16, 2020

@foolin That's my point I don't want to need to add it to Render on every single Handler, here it's what I did, wdyt ?

// Template structure
type Template struct {
	ev *echoview.ViewEngine
}

// Render template
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {

	// Add global methods if data is a map
	if viewContext, isMap := data.(echo.Map); isMap {
		viewContext["newVariable"] = "MyValue" // Here I can get the value from context
	}

	return t.ev.Render(w, name, data, c)
}

@foolin
Copy link
Owner

foolin commented Aug 16, 2020

Well, that's a good way.

@wsantos
Copy link
Contributor Author

wsantos commented Aug 16, 2020

Thank you @foolin

@wsantos wsantos closed this as completed Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants