mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
2805a6f80e
commit
35bb72c83e
2 changed files with 13 additions and 1 deletions
|
@ -60,7 +60,13 @@ func (scp *ShortcodeWithPage) Get(key interface{}) interface{} {
|
||||||
if reflect.TypeOf(scp.Params).Kind() == reflect.Map {
|
if reflect.TypeOf(scp.Params).Kind() == reflect.Map {
|
||||||
return "error: cannot access named params by position"
|
return "error: cannot access named params by position"
|
||||||
} else if reflect.TypeOf(scp.Params).Kind() == reflect.Slice {
|
} else if reflect.TypeOf(scp.Params).Kind() == reflect.Slice {
|
||||||
x = reflect.ValueOf(scp.Params).Index(int(reflect.ValueOf(key).Int()))
|
idx := int(reflect.ValueOf(key).Int())
|
||||||
|
ln := reflect.ValueOf(scp.Params).Len()
|
||||||
|
if idx > ln-1 {
|
||||||
|
helpers.DistinctErrorLog.Printf("No shortcode param at .Get %d in page %s, have params: %v", idx, scp.Page.FullFilePath(), scp.Params)
|
||||||
|
return fmt.Sprintf("error: index out of range for positional param at position %d", idx)
|
||||||
|
}
|
||||||
|
x = reflect.ValueOf(scp.Params).Index(idx)
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
if reflect.TypeOf(scp.Params).Kind() == reflect.Map {
|
if reflect.TypeOf(scp.Params).Kind() == reflect.Map {
|
||||||
|
|
|
@ -85,6 +85,12 @@ func TestPositionalParamSC(t *testing.T) {
|
||||||
CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", tem)
|
CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", tem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPositionalParamIndexOutOfBounds(t *testing.T) {
|
||||||
|
tem := tpl.New()
|
||||||
|
tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 1 }}`)
|
||||||
|
CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video error: index out of range for positional param at position 1", tem)
|
||||||
|
}
|
||||||
|
|
||||||
func TestNamedParamSC(t *testing.T) {
|
func TestNamedParamSC(t *testing.T) {
|
||||||
tem := tpl.New()
|
tem := tpl.New()
|
||||||
tem.AddInternalShortcode("img.html", `<img{{ with .Get "src" }} src="{{.}}"{{end}}{{with .Get "class"}} class="{{.}}"{{end}}>`)
|
tem.AddInternalShortcode("img.html", `<img{{ with .Get "src" }} src="{{.}}"{{end}}{{with .Get "class"}} class="{{.}}"{{end}}>`)
|
||||||
|
|
Loading…
Reference in a new issue