gobuffalo / plush

The powerful template system that Go needs
MIT License
901 stars 56 forks source link

Custom Block Helpers not working #111

Closed r3ap3r2004 closed 2 years ago

r3ap3r2004 commented 4 years ago

Custom block helpers are not working for me. I am following the documentation found here: https://gobuffalo.io/en/docs/custom-helpers/#block-helpers

If I try the exact block helper example shown in the document I get the following compile error:

actions/render.go:39:27: cannot use strings.ToUpper(s) (type string) as type "html/template".HTML in return argument

the original helper code

func upblock(help plush.HelperContext) (template.HTML, error) {
  s, err := help.Block()
  if err != nil {
    return "", err
  }
  return strings.ToUpper(s), nil
}

That is obviously caused by a bug in the example code where it is expecting a template.HTML response, but the example code is only returning a plain string. So I changed the upblock function to return a string instead, but now I get the following error:

reflect: Call using plush.HelperContext as type plush.HelperContext

Any idea what is going wrong? I have also tried the reverse of leaving the return value as template.HTML and converting the output to use a buffalo tag, but that still has the same error.

My code looks like this:

render.go

func init() {
    r = render.New(render.Options{
        // HTML layout to be used for all HTML requests:
        HTMLLayout: "application.plush.html",

        // Box containing all of the templates:
        TemplatesBox: packr.New("app:templates", "../templates"),
        AssetsBox:    assetsBox,

        // Add template helpers here:
        Helpers: render.Helpers{
            // for non-bootstrap form helpers uncomment the lines
            // below and import "github.com/gobuffalo/helpers/forms"
            // forms.FormKey:     forms.Form,
            // forms.FormForKey:  forms.FormFor,
            "upblock": func(help plush.HelperContext) (string, error) {
                s, err := help.Block()
                if err != nil {
                    return "", err
                }
                return strings.ToUpper(s), nil
            },
        },
    })
}

index.plush.html

<%= upblock() { %>
  hello world
<% } %>

go.mod

module myapp

go 1.13

require (
    github.com/aws/aws-sdk-go v1.30.13
    github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0 // indirect
    github.com/benbjohnson/clock v1.0.1 // indirect
    github.com/casbin/casbin/v2 v2.2.2
    github.com/gobuffalo/buffalo v0.16.8
    github.com/gobuffalo/buffalo-pop v1.23.1
    github.com/gobuffalo/envy v1.9.0
    github.com/gobuffalo/events v1.4.1
    github.com/gobuffalo/mw-csrf v1.0.0
    github.com/gobuffalo/mw-forcessl v0.0.0-20200131175327-94b2bd771862
    github.com/gobuffalo/mw-i18n v1.1.0
    github.com/gobuffalo/mw-paramlogger v1.0.0
    github.com/gobuffalo/nulls v0.4.0
    github.com/gobuffalo/packr/v2 v2.8.0
    github.com/gobuffalo/plush v3.8.3+incompatible
    github.com/gobuffalo/pop v4.13.1+incompatible
    github.com/gobuffalo/suite v2.8.2+incompatible
    github.com/gobuffalo/tags v2.1.7+incompatible
    github.com/gobuffalo/validate v2.0.4+incompatible
    github.com/gobuffalo/x v0.0.0-20190224155809-6bb134105960
    github.com/gofrs/uuid v3.2.0+incompatible
    github.com/guregu/dynamo v1.7.0
    github.com/markbates/goth v1.64.0
    github.com/pkg/errors v0.9.1
    github.com/unrolled/secure v1.0.7
    gopkg.in/auth0.v4 v4.3.6
)

go version

✗ go version
go version go1.13.6 darwin/amd64
agbaraka commented 4 years ago

Same here. I had similar issue.

But after changing the method parameter type from plush.HelperContext to hctx.HelperContext resolved this problem

PS: I found this after digging into default helpers (i.e forms.Form)

@markbates Please confirm if the above is the expected signature so that I can send a pull request to update the documentation accordingly.

Thanks.

breml commented 4 years ago

I had the same problem and error message: reflect: Call using plush.HelperContext as type plush.HelperContext.

I resolved it by changing the import for github.com/gobuffalo/plush to github.com/gobuffalo/plush/v4.

breml commented 4 years ago

See https://github.com/OpenPeeDeeP/depguard/issues/9#issuecomment-657441364 for a hint, on how these situations can be detected by the depguard linter (also included in golangci-lint).

sio4 commented 2 years ago

It seems like this question has a helpful answer. I am closing it.