Module:If elseif
| File:Greek lc beta icon.svg | This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
Implements {{If elseif}}. Tests a series of expressions, which are evaluated either as blank/noblank or, if |yesno= is set, using {{Yesno}}. Functionally equivalent to a if...elseif...elseif...else string butore efficient than nesting {{#if:}} parser functions from a post-expand include size standpoint.
Usage
{{#if:test 1|value 1|{{#if:test 2|value 2|{{#if:...|default value}}}}}} can be replaced with
{{#invoke:if elseif||test 1|value 1|test 2|value 2|...|default value}} or
{{#invoke:if elseif||if1 = test 1|then1 = value 1|if2 = test 2|then2 = value 2|...|else = default value}}
Examples
"{{#invoke:if elseif||{{{empty1|}}}|1|{{{empty2|}}}|2|all empty}}"→ "all empty"
"{{#invoke:if elseif||if1={{{empty1|}}}|then1=1|if2={{{notempty2|somevalue}}}|then2=2|else=all empty}}"→ "2"
"{{#invoke:if elseif||{{#expr:1<0}}|1<0|{{#expr:4<0}}|4<0|{{#expr:-1<0}}|-1<0|{{#expr:-2<0}}|-2<0|None are true|yesno=true}}"→ "-1<0"- Note the use of
|yesno=true. Without it, the module returns "1<0" since #expr returns0when the statement is false, which is not blank.
- Note the use of
p = {}
local yesno = require('Module:Yesno')
p['_elseif'] = function(args)
local n = 1
repeat
local ifval, thenval
if args[n] then
if args[n+1] then
ifval, thenval = args[n], args[n+1]
n = n + 2
else
return args[n]
end
elseif args['if' .. n] and args['then' .. n] then
ifval, thenval = args['if' .. n], args['then' .. n]
n = n + 1
else
return args['else'] or ""
end
if yesno(args.yesno) then
if yesno(ifval) then return thenval end
elseif mw.text.trim(ifval or '') ~= '' then
return thenval
end
until false
end
p['elseif'] = function(frame)
local args = require('Module:Arguments').getArgs(frame, {trim = false, removeBlanks = false})
return p['_elseif'](args)
end
p[''] = p['elseif']
return p