Module:XForLoopDo

Ka Wikipedia

Documentation for this module may be created at Module:XForLoopDo/doc

-- This module implements {{for loop}}.

local getArgs = require('Module:Arguments').getArgs

p = {}

function p.main(frame)
	local args = getArgs(frame, {
		trim = false,
		removeBlanks = false
	})
	return p._main(args)
end

function p._main(args)
	local template = args['__call'] or 'void'
	local ifrom = tonumber(args['__from']) or 1
	local ito = tonumber(args['__to']) or 10

	local sep = args['__sep'] or ""

	local result = ''
	local j = ifrom
	while (j<= ito) do
          local targs = {}
          for i, v in ipairs(args) do
		    v = mw.text.trim(v) -- trim whitespace		
                    targs[i] = mw.ustring.gsub( v, "\$i", tostring(j) )
          end
          for i, v in pairs(args) do
		    v = mw.text.trim(v) -- trim whitespace		
                    targs[i] = mw.ustring.gsub( v, "\$i", tostring(j) )
          end
          local expandedTemplate = p.callTemplate(template, targs)
	  result = result .. expandedTemplate
	  if( j < ito) then
	     result = result .. sep
	  end
          j = j + 1
	end
	return result
end

function p.callTemplate(template, targs)
	return mw.getCurrentFrame():expandTemplate{title = template, args = targs}
end

return p