Numbered query templates

Is there a way to have a query template print a number sequence? I.e.

  1. First
  2. Second
  3. Third

I did it by copying the template.each function into my namespace and adding the existing iterator count to the render context object:

function asmw.each(tbl, fn)
  local result = {}
  for i, item in ipairs(tbl) do
      item._i = i
      table.insert(result, fn(item))
  end
  return table.concat(result)
end

I can then use a template like:

asmw.testItem = template.new([==[${_i}. ${ref}]==])
2 Likes