Shortcode

Position

返回调用该短代码的文件名和位置。

签名
SHORTCODE.Position
返回类型
text.Position

Position 方法在报告错误时很有用。例如,如果你的短代码需要一个「greeting」参数:

layouts/_shortcodes/myshortcode.html
{{ $greeting := "" }}
{{ with .Get "greeting" }}
  {{ $greeting = . }}
{{ else }}
  {{ errorf "The %q shortcode requires a 'greeting' argument. See %s" .Name .Position }}
{{ end }}

在没有「greeting」参数时,Hugo 会抛出错误消息并让构建失败:

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