Problem with <ref> in shortcode

Hi,

I created a pretty simple shortcode:

themes/mytheme/layout/shortcodes/ref_pg_abc.html
[A B C (ABC)]({{ ref "/pages/abc/home.md" }})

Then I use the shortcode:

content/page/pg1/index.md
... The current {{<ref_pg_abc>}} user ...

I get the error:

".../content/page/pg1/index.md:27:13": failed to render shortcode "ref_pg_abc": failed to process shortcode: "...themes/mytheme/layouts/shortcodes/ref_pg_abc.html:1:46": execute of template failed: template: shortcodes/ref_pg_abc.html:1:46: executing "shortcodes/ref_pg_abc.html" at <ref>: wrong number of args for ref: want 2 got 1

I’ve tried both {{< and {{% with no success.

Any ideas?

Thanks,

Barry

Hi @BigBadBaz. We cannot help you with only this info. You’ll need to share your repo, or create a minimal reproducible example and share that.

Without the < or %, you’re calling a function, not a shortcode. The function “ref” is used in the shortcode “ref”, but they are not the same thing. Try including the < or % in the source of the shortcode. If that doesn’t help, drop the < or % and read the rest of my post.

According to the documentation at https://gohugo.io/functions/ref/, the function needs a Context. That’s the argument you’re missing https://gohugo.io/templates/introduction/#the-dot

You need to work out the context that works here, I have to admit I’m not quite familiar with using that in practice. The dot should work though, try that first.

1 Like

Thank you! That was enough of a hint to get me going. For others following along:

I changed my shortcode themes/mytheme/layout/shortcodes/ref_pg_abc.html from:

[A B C (ABC)]({{ ref "/pages/abc/home.md" }})

to

[A B C (ABC)]({{ ref . "/pages/abc/home.md" }})

Which solved the ref issue, but displayed text as opposed to rendering the markdown. So I changed the markdown file content/page/pg1/index.md from:

... The current {{<ref_pg_abc>}} user ...

to

... The current {{%ref_pg_abc%}} user ...