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

Allow injecting into helper functions #3

Open
codegangsta opened this issue Feb 8, 2014 · 6 comments
Open

Allow injecting into helper functions #3

codegangsta opened this issue Feb 8, 2014 · 6 comments

Comments

@codegangsta
Copy link
Member

from @ahall

The goal is to be able to do something like this:

m.Use(render.Renderer(render.Options{
    Layout:    "layout",
    Funcs: []template.FuncMap{
        {
            "myCustomFunc": func(r *http.Request, m MyInjectedStuff) string {
                                    // Do something with r and m
                return "My custom function"
            },
        },
    },
}))

Original issue:
https://github.com/codegangsta/martini-contrib/issues/99

@ahall
Copy link

ahall commented Mar 28, 2014

Hi @codegangsta. Have you had time to think about this?

@sevkin
Copy link

sevkin commented Apr 7, 2014

very useful. it will be possible to inject render.Render?

@seedifferently
Copy link

Definitely needed. I'd also like to be able to inject values from sessions.Session.

@ahall
Copy link

ahall commented Apr 11, 2014

Yep, now just waiting feedback from the mighty @codegangsta

@codegangsta
Copy link
Member Author

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.

@scottkf
Copy link

scottkf commented Apr 11, 2014

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
        }
    }
}

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

5 participants