plush icon indicating copy to clipboard operation
plush copied to clipboard

Custom Block Helpers not working

Open r3ap3r2004 opened this issue 5 years ago • 3 comments

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

r3ap3r2004 avatar May 17 '20 05:05 r3ap3r2004

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.

agbaraka avatar Jun 14 '20 16:06 agbaraka

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 avatar Jul 13 '20 08:07 breml

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).

breml avatar Jul 13 '20 09:07 breml

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

sio4 avatar Sep 04 '22 14:09 sio4