模組:Loop
外观

給wikitext使用的簡易迴圈,目前只有遞增型和遞減型兩種
使用方法
{{#invoke:Loop|increment|...arguments...}}
{{#invoke:Loop|decrement|...arguments...}}
start
:開始循環的數字。end
:結束循環的數字。range
:每次循環增加或減去的數字。content
:循環的內容。delnowiki
、delmsgnw
:如其名。usingConditionalExpressions
:去除循環內的跳脫字元,參見Module:TemplateParameters#FormatingArguments。
- 範例:
{{#invoke:Loop|increment
|start=1|end=10|range=1|delnowiki=1
|content=<nowiki>* 第{{{1}}}次,你好!</nowiki>
}}
- 或
{{#invoke:Loop|increment
|start=1|end=10|range=1|usingConditionalExpressions=1
|content=* 第\{\{\{1\}\}\}次,你好!\n
}}
- 結果:
脚本错误:函数“increment”不存在。
其他可用在content的變數:
{{{last}}}
:上一個數字,若不存在則為空值。{{{current}}}
:等同{{{1}}}
。{{{next}}}
:下一個數字,若不存在則為空值。
理論上也可以傳入浮點數,但如果出現了奇怪的行為的話建議使用整數。
local p = {}
local yesno
local mTemplateParameters
function p.main(frame)
local args = frame.args
local loopStart = tonumber(args['start']) or 0
local loopEnd = tonumber(args['end']) or 0
local loopContent = args.content
if loopStart == loopEnd or not loopContent then
return ''
end
local loopRange = tonumber(args['range']) or 1
if loopStart > loopEnd or loopRange < 0 then
error('Only increment syntax allowed.')
end
if not yesno then
yesno = require('Module:Yesno')
end
if yesno(args.delnowiki) then
loopContent = mw.text.unstripNoWiki(loopContent)
end
if yesno(args.delmsgnw) then
loopContent = mw.text.decode(loopContent)
end
if yesno(args.usingConditionalExpressions) then
if not mTemplateParameters then
mTemplateParameters = require('Module:TemplateParameters')
end
loopContent = mTemplateParameters._get_escape(loopContent)
end
local i = loopStart
local output = ''
while true do
output = output .. loopContent:gsub('{{{1}}}', tostring(i))
i = i + loopRange
if i > loopEnd then
break
end
end
return mw.getCurrentFrame():preprocess(output)
end
return p