Hugo: How to generate a random ID or use parameter in shortcode
In Hugo, you can generate a random ID string like this:
hugo_random_id.go
{{ $seed := now.UnixNano }}
{{ $randomId := printf "%.10s" (sha256 $seed) }}
You can use the random ID directly, or use a shortcode parameter to override it:
example.md
{{< my_shortcode id="my-id" >}}
example.html
{{/* $seed := now.Unix */}}
{{/* $randomId := printf "%.10s" (sha256 $seed) */}}
{{/* $id := .Get "id" | default $randomId */}}
<!-- Usage example -->
<div id="{{ $id }}">Hello, world!</div>
Check out similar posts by category:
Hugo
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow