IsNamedParams
為了支援在呼叫短碼時同時使用位置參數與命名參數,可以使用 IsNamedParams
方法來判斷短碼是如何被呼叫的。
使用以下短碼範本:
layouts/shortcodes/myshortcode.html
{{ if .IsNamedParams }}
{{ printf "%s %s." (.Get "greeting") (.Get "firstName") }}
{{ else }}
{{ printf "%s %s." (.Get 0) (.Get 1) }}
{{ end }}
以下兩個呼叫會返回相同的值:
content/about.md
{{< myshortcode greeting="Hello" firstName="world" >}}
{{< myshortcode "Hello" "world" >}}