-
Notifications
You must be signed in to change notification settings - Fork 57
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
Allow injecting into helper functions #3
Comments
Hi @codegangsta. Have you had time to think about this? |
very useful. it will be possible to inject render.Render? |
Definitely needed. I'd also like to be able to inject values from |
Yep, now just waiting feedback from the mighty @codegangsta |
Yeah this would be great to have. There are some unfortunate limitation as to how Go's template library works with helper functions. There may be some clever ways to do so. But most of the solutions seem overly hacky. |
The only thing that appears to work is defining a stub method, then including a middleware overriding the template maps (which injects anything I need, like session, etc). The only thing that might make it easier is not having to define the method in the beginning, but golang's text/template doesn't appear to work that way. Might be some hackish way to implement it though. m.Use(render.Renderer(render.Options{
Extensions: []string{".tmpl", ".html"},
Funcs: funcHeader}))
m.Use(HelperFuncs())
var funcHeader = []template.FuncMap{
{
"set_data": func(context map[string]interface{}) string {
return ""
},
}
}
func HelperFuncs() martini.Handler {
return func(r render.Render, u *users.UserProfile, s sessions.Session) {
r.Template().Funcs(funcDefinitions(u, s))
}
}
var funcDefinitions = func(u *users.UserProfile, s sessions.Session) template.FuncMap {
return template.FuncMap{
"set_data": func(context map[string]interface{}) string {
do something with session
}
}
} |
from @ahall
The goal is to be able to do something like this:
Original issue:
https://github.com/codegangsta/martini-contrib/issues/99
The text was updated successfully, but these errors were encountered: