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

"is not a method but has arguments" error message is confusing #10862

Closed
willfaught opened this issue Mar 24, 2023 · 13 comments
Closed

"is not a method but has arguments" error message is confusing #10862

willfaught opened this issue Mar 24, 2023 · 13 comments

Comments

@willfaught
Copy link

With this template:

x {{ .Param "foo" }} y

and this config.yaml:

params:
  foo: bar

I got this error:

EXECUTE-AS-TEMPLATE: failed to transform "foo.html" (text/html) at <.Param>: Param is not a method but has arguments 

I have no idea what this error message means.

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.111.3+extended darwin/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes

@pamubay
Copy link

pamubay commented Mar 24, 2023

.Param is Page method, make sure you pass the Page as context when using resources.ExecuteAsTemplate.

Works, . is Page.

{{ $tmpl := `x {{ .Param "foo" }} y` | resources.FromString "foo.tmpl" }}
{{ $exe := $tmpl | resources.ExecuteAsTemplate "foo.html" . }}
{{ $exe.RelPermalink }}

Error, passing (dict) as context.

{{ $tmpl := `x {{ .Param "foo" }} y` | resources.FromString "foo.tmpl" }}
{{ $exe := $tmpl | resources.ExecuteAsTemplate "foo.html" (dict) }}
{{ $exe.RelPermalink }}

Error:

Error: Error building site: EXECUTE-AS-TEMPLATE: failed to transform "foo.tmpl" (/): template: foo.tmpl:1:5: executing "foo.tmpl" at <.Param>: Param is not a method but has arguments

@willfaught
Copy link
Author

Thanks, but I still don't understand the message. What sort of Foo.Bar is not a method but has arguments?

@bep bep added Upstream and removed NeedsTriage labels Mar 29, 2023
@bep bep added this to the v0.112.0 milestone Mar 29, 2023
@bep
Copy link
Member

bep commented Mar 29, 2023

The error comes from here:

https://github.com/golang/go/blob/master/src/text/template/exec.go#L669

I agree that it would be useful to have the receiver type (a map of some sort) in the error message, but I'm afraid you need to convince the Go people to get that fix.

@willfaught
Copy link
Author

To summarize, the issue is that the template context is a map without a matching key? Do I have that right? So any time a map doesn't have a key, text/template produces a "[key] is not a method but has arguments" error?

@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@stefanv
Copy link

stefanv commented May 23, 2023

This is also an issue that arose for us with the latest Hugo release (0.111.3 works, 0.112.1 doesn't). I'm not sure the context above is applicable to our case. I have:

    {{ $meta_fields := (slice "author" "discussion" "history") }}
    {{- range $attr := $meta_fields -}}
      {{ $val := $.Params.Get $attr }}```
...

The error is:

<$.Params.Get>: Get is not a method but has arguments

But, it looks like we always provide an argument to Get?

@bep
Copy link
Member

bep commented May 23, 2023

But, it looks like we always provide an argument to Get?

I haven't looked closely, but I suspect you have become a victim of my "API spring cleaning" project. A lot of work was put into getting some control over "what is the public API" of Hugo. I don't remember why that particular method was removed, but I don't see why you cannot just do:

 {{ $val := index $.Params $attr }}

which is a construct that works for all maps.

@stefanv
Copy link

stefanv commented May 23, 2023

Thanks! That should be easy to fix then. Since the removed "method" names are known, perhaps it would be useful to add error messages to catch common mistakes like this.

stefanv added a commit to stefanv/scientific-python.org that referenced this issue May 23, 2023
`.Params.Get $key` has been deprecated; use `index .Params $key`
instead.

See gohugoio/hugo#10862 (comment)
stefanv added a commit to scientific-python/scientific-python.org that referenced this issue May 23, 2023
`.Params.Get $key` has been deprecated; use `index .Params $key`
instead.

See gohugoio/hugo#10862 (comment)
nanxiaobei added a commit to nanxiaobei/hugo-paper that referenced this issue May 28, 2023
@bep bep modified the milestones: v0.113.0, v0.114.0, v0.115.0 Jun 8, 2023
@SudoCerb
Copy link

Just got back into Hugo as I have new things to add to my website - but I'm running into this error, followed the suggestions above but to no avail.

I last built my site using v0.109:

hugo v0.109.0-47b12b83e636224e5e601813ff3e6790c191e371+extended windows/amd64 BuildDate=2022-12-23T10:38:11Z VendorInfo=gohugoio

I have just upgraded to the latest version available on chocolatey:

hugo v0.115.0-67caf50698783d3b3d78559ac81483d83e5e1a35+extended windows/amd64 BuildDate=2023-06-29T15:56:39Z VendorInfo=gohugoio

My original code:

{{- $fnStart := int (.Page.Params.Get "fnStart") -}}

I've got a multi-page site that's essentially a book, and the footnote starting numbers are defined by a page parameter (in lieu of a some smarter solution, for now).

This code produces <.Page.Params.Get>: Get is not a method but has arguments.

I have tried: {{- $fnStart := int (index $.Params "fnStart") -}} but get this error:

executing "shortcodes/footnote.html" at <index $.Params "fnStart">: error calling index: index of type []string with args [fnStart] failed: cannot index slice/array with type string

Apologies I'm a little out of my depth on this syntax and would appreciate any help I can get!

@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@jmooring
Copy link
Member

@SudoCerb Change this:

{{ .Page.Params.Get "fnStart" }}

to either of these:

{{ index .Page.Params "fnStart" }}
{{ .Page.Params.fnStart }}

Although the .Get method on .Page.Params worked prior to v0.112.0, it was never documented, and has been removed from the API.

@bep
Copy link
Member

bep commented Jun 30, 2023

I'm closing this, as this i'nt likely to be reverted.

@bep bep closed this as completed Jun 30, 2023
@stefanv
Copy link

stefanv commented Jun 30, 2023

Any hope to update the cryptic error message? This is under the assumption that "Get" can easily be detected, but perhaps not.

@SudoCerb
Copy link

SudoCerb commented Jul 1, 2023

@SudoCerb Change this:

{{ .Page.Params.Get "fnStart" }}

to either of these:

{{ index .Page.Params "fnStart" }}
{{ .Page.Params.fnStart }}

Although the .Get method on .Page.Params worked prior to v0.112.0, it was never documented, and has been removed from the API.

Thanks very much! This solved that issue!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants