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

Handle passing functions to templates? #2

Open
jonathanGB opened this issue Feb 23, 2017 · 7 comments
Open

Handle passing functions to templates? #2

jonathanGB opened this issue Feb 23, 2017 · 7 comments

Comments

@jonathanGB
Copy link

I know it's possible to pass helper functions to normal html/template, but is it possible with this wrapper? If not, it would be cool to have it.

Thanks!

@BenKnigge
Copy link

I'm also trying to figure out how to get this working. No matter what I do I end up getting an error that the function is not defined.

@Macilias
Copy link

Macilias commented Jul 4, 2018

My guess is that the randerer treats each comment without leading . as function. This really is an issue, since it interferes with 3rd party stuff including {{tobereplaced}} stuff like dropbox for example.
The only solution I could think of here would be to change the delimiter for mulitemplate, but the expample from gin-gonic: router.Delims("{[{", "}]}") seam not to have any effect :(

@nanom1t
Copy link

nanom1t commented Jan 4, 2019

Any updates?

@sergolius
Copy link

There is two variants how to add Funcs:

  • AddFromFilesFuncs;
  • AddFromStringsFuncs.
    Lets use .../multitemplate/example/advanced/example.go:45 as base:
r.AddFromFiles(filepath.Base(include), files...)

AddFromFilesFuncs:

r.AddFromFilesFuncs(filepath.Base(include), template.FuncMap{}, files...)

AddFromStringsFuncs:

r.AddFromStringsFuncs("tmpl-name", template.FuncMap{}, `<script>alert(1);</script>`)

@asbjornu
Copy link

How does any of those methods make the registered functions available within the (HTML) templates, @sergolius?

@sergolius
Copy link

@asbjornu AddFromFilesFuncs, AddFromStringsFuncs uses Go's package html/template functionality of Template creation and extension.
AddFromStringsFuncs:

tmpl := template.New(name).Funcs(funcMap)

@asbjornu
Copy link

@sergolius, ah, I see. I managed to get this to work:

r := multitemplate.NewRenderer()
r.AddFromFilesFuncs("entry", template.FuncMap{
    "my_func": func() string {
        return "xyz"
    },
}, "views/base.html", "views/entry.html")

I am now able to use {{ my_func }} in entry.html, which outputs xyz. Thanks!

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

6 participants