Hugo:如何在 shortcode 中生成随机 ID 或使用参数

在 Hugo 中,你可以像这样生成随机 ID 字符串:

hugo_random_id.go
{{ $seed := now.UnixNano }}
{{ $randomId := printf "%.10s" (sha256 $seed) }}

你可以直接使用随机 ID,或使用 shortcode 参数覆盖它:

shortcode_usage.md
{{< my_shortcode id="my-id" >}}
shortcode_template.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