Errorf/warnf will trigger in unused inline shortcodes

I think I’ve encountered a bug, but I’d like to double check. I attempted to add validation to an inline shortcode using errorf and it caused Hugo to raise an error even though the shortcode was used correctly.

Here’s a minimal repro. The presence of this inline shortcode in a content file will cause a warning, even though it’s never used.

{{< test.inline >}}
	{{ with .Get "Foo" }}
	{{ else }}
		{{ warnf "Missing parameter" }}
	{{ end }}
{{< /test.inline >}}

It seems that the shortcode is invoked even though it is never used. And since that invocation doesn’t provide parameters it cause the warning to fire.

Hugo Extended 0.124.1

When you define an inline shortcode you are calling it at the same time.

Oh, that makes more sense. That’s more clear upon re-reading the docs.

Is it possible to pass arguments to the definition of an inline shortcode? It’s not discussed in the docs. I can definitely pass arguments in subsequent uses, but I don’t know the syntax to do so in the initial definition.

{{< test.inline foo="bar" >}}
	{{ with .Get "foo" }}
		{{ warnf . }}
	{{ end }}
{{< /test.inline >}}

Thanks

Do inline shortcodes support .Inner? This second use of the shortcode doesn’t work, despite being the same syntax that works when the shortcode isn’t inlined.

{{< test.inline >}}
	{{ with (.Get 0) }}
		{{ . }}
	{{ else }}
		nope
	{{ end }}
{{< /test.inline >}}

{{< test.inline "foo" >}}
asd
{{< /test.inline >}}

No. In the example above, the second redefines the first.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.