hugo/content/en/methods/shortcode/Position.md
Bjørn Erik Pedersen 9b0050e9aa Squashed 'docs/' content from commit 5c085a37b
git-subtree-dir: docs
git-subtree-split: 5c085a37b297bf12f59efeaae591418ec025c10d
2024-01-27 10:48:33 +01:00

973 B

title description categories keywords action
Position Returns the filename and position from which the shortcode was called.
related returnType signatures
methods/shortcode/Name
functions/fmt/Errorf
text.Position
SHORTCODE.Position

The Position method is useful for error reporting. For example, if your shortcode requires a "greeting" parameter:

{{< code file=layouts/shortcodes/myshortcode.html >}} {{ $greeting := "" }} {{ with .Get "greeting" }} {{ $greeting = . }} {{ else }} {{ errorf "The %q shortcode requires a 'greeting' parameter. See %s" .Name .Position }} {{ end }} {{< /code >}}

In the absence of a "greeting" parameter, Hugo will throw an error message and fail the build:

ERROR The "myshortcode" shortcode requires a 'greeting' parameter. See "/home/user/project/content/about.md:11:1"

{{% note %}} The position can be expensive to calculate. Limit its use to error reporting. {{% /note %}}