docs/content/en/methods/shortcode/Ordinal.md
The Ordinal method returns the zero-based ordinal of the shortcode in relation to its parent. If the parent is the page itself, the ordinal represents the position of this shortcode in the page content.
[!note] Hugo increments the ordinal with each shortcode call, regardless of the specific shortcode type. This means that the ordinal value is tracked sequentially across all shortcodes within a given page.
This method is useful for, among other things, assigning unique element IDs when a shortcode is called two or more times from the same page. For example:
{{</* img src="images/a.jpg" */>}}
{{</* img src="images/b.jpg" */>}}
This shortcode performs error checking, then renders an HTML img element with a unique id attribute:
{{ $src := "" }}
{{ with .Get "src" }}
{{ $src = . }}
{{ with resources.Get $src }}
{{ $id := printf "img-%03d" $.Ordinal }}
{{ else }}
{{ errorf "The %q shortcode was unable to find %s. See %s" $.Name $src $.Position }}
{{ end }}
{{ else }}
{{ errorf "The %q shortcode requires a 'src' argument. See %s" .Name .Position }}
{{ end }}
Hugo renders the page to:
[!note] In the shortcode template above, the
withstatement is used to create conditional blocks. Remember that thewithstatement binds context (the dot) to its expression. Inside of awithblock, preface shortcode method calls with a$to access the top-level context passed into the template.