Variables as an object method / Dynamically creating function method name

Hello Hugo community,

Perhaps someone might be able to answer my question. I have created a shortcode prints out a table here: https://gist.github.com/fieldingtron/1530c23a21df29052e2afac26d9af09d

the shortcode ranges over a front matter parameter
.
range $.Page.Params.table

my question is the following – is there way to pass in the variable to range over ?

I have tried

{{ range range (printf “%s%s” “$.Page.Params.” (.Get 0) ) }} to select another table to print out

but I keep getting the error
execute of template failed: template: shortcodes/table2.html:7:47: executing “shortcodes/table2.html” at <0>: range can’t iterate over $.Page.Params.table

Is there a way to dynamically create a function method ?

Any thoughts ?

Thanks in advance anyone

Try $.Page.Param (.Get 0)

(docs)

1 Like

thanks for your quick reply … same error message this time with one letter different

template: shortcodes/table2.html:7:46: executing “shortcodes/table2.html” at <0>: range can’t iterate over $.Page.Param.table

my shortcode looks like this below

{{<table2 “table”>}}

and the frontmatter field looks like this …

table: 
-
  - Name
  - Address
  - Location

The shortcode works well when I set the field in code like this below

range $.Page.Params.table

Make sure you don’t have quotes around the param. I tried with your template and it works for me, when I replace:

{{ range $.Page.Param.table }}

with

{{ range $.Page.Param (.Get 0) }}

Note that you can format your table data better by using yaml in-line flow sequence style:

table:
  - [Name, Address, Location]
  - [A,    B,       C]
1 Like