mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Add shortcode IsNamedParams property
It would be helpful to know whether a shortcode was called with positional or named parameters. This commit adds a boolean `IsNamedParams` property to the `ShortcodeWithPage` struct.
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
3d09de8910
commit
fde47c5eb9
@@ -119,6 +119,20 @@ func TestNamedParamSC(t *testing.T) {
|
||||
CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, `<img src="one" class="aspen grove">`, tem)
|
||||
}
|
||||
|
||||
func TestIsNamedParamsSC(t *testing.T) {
|
||||
tem := tpl.New()
|
||||
tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`)
|
||||
tem.AddInternalShortcode("byname.html", `<div id="{{ .Get "id" }}">`)
|
||||
tem.AddInternalShortcode("ifnamedparams.html", `<div id="{{ if .IsNamedParams }}{{ .Get "id" }}{{ else }}{{ .Get 0 }}{{end}}">`)
|
||||
|
||||
CheckShortCodeMatch(t, `{{< ifnamedparams id="name" >}}`, `<div id="name">`, tem)
|
||||
CheckShortCodeMatch(t, `{{< ifnamedparams position >}}`, `<div id="position">`, tem)
|
||||
CheckShortCodeMatch(t, `{{< byname id="name" >}}`, `<div id="name">`, tem)
|
||||
CheckShortCodeMatch(t, `{{< byname position >}}`, `<div id="error: cannot access positional params by string name">`, tem)
|
||||
CheckShortCodeMatch(t, `{{< byposition position >}}`, `<div id="position">`, tem)
|
||||
CheckShortCodeMatch(t, `{{< byposition id="name" >}}`, `<div id="error: cannot access named params by position">`, tem)
|
||||
}
|
||||
|
||||
func TestInnerSC(t *testing.T) {
|
||||
tem := tpl.New()
|
||||
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)
|
||||
|
Reference in New Issue
Block a user