Capefeather (talk | contribs) (Fixed the parameters of the for loop to make the start variable do what it's supposed to) Tag: Undo |
Capefeather (talk | contribs) (Added number of params to skip) |
||
Line 9: | Line 9: | ||
local start = iterArgs["start"] or 1 |
local start = iterArgs["start"] or 1 |
||
local finish = iterArgs["end"] or 1 |
local finish = iterArgs["end"] or 1 |
||
+ | local skip = iterArgs["skip"] or 0 |
||
local params = {} |
local params = {} |
||
− | for i = 1, finish - start + 1 do |
+ | for i = 1 + skip, finish - start + 1 + skip do |
params[i] = i + start - 1 |
params[i] = i + start - 1 |
||
end |
end |
Revision as of 00:14, 18 January 2021
Lua functions that generate the equivalent of "iterator objects" to help automate putting parameters into a template call in some cases.
Range
Generates a list of numbers from a starting point to an ending point to feed into a template. Meant to imitate range() in Python.
Usage
{{#invoke:iterator|range|example|end=6}} is the same as {{example|1|2|3|4|5|6}}.
local p = {}
function p.range(frame)
-- Generates a list of numbers from a starting point to an ending point to feed into a template.
-- e.g. {{#invoke:iterator|range|example|end=6}} is the same as {{example|1|2|3|4|5|6}}
local iterArgs = frame:getParent().args
local tname = iterArgs["tname"]
local start = iterArgs["start"] or 1
local finish = iterArgs["end"] or 1
local skip = iterArgs["skip"] or 0
local params = {}
for i = 1 + skip, finish - start + 1 + skip do
params[i] = i + start - 1
end
for k,v in pairs(iterArgs) do
params[k] = v
end
return frame:expandTemplate{title = tname, args = params}
end
return p
--[[Category:Modules]]