模块:Yesno

来自SINO云课堂
TeCHiScy讨论 | 贡献2018年5月8日 (二) 14:38的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)


This template normalises an input to be a yes/no output.

Usage

The template has five possible outputs depending on the default parameter (input if you will).

In its simplest usage these will all be either "yes" or "" (blank - nothing no characters).

  1. {{yesno|yes}} result "yes" (also applies to "Yes", "YeS", etc, "Y", "y" and "1")
  2. {{yesno|no}} result "" (also applies to "No", "NO", "nO", "N", "n" and "0")
  3. {{yesno}} result ""
  4. {{yesno|¬}} result ""
  5. {{yesno|purplemonkeydishwasher}} result "yes" (also applies to any other value not given above).


Each of these can be over-ridden.

  1. {{yesno|yes|yes=bacon}} result "bacon" (also applies to "Yes", "YeS", etc, "Y", "y" and "1")
  2. {{yesno|no|no=ham}} result "ham" (also applies to "No", "NO", "nO", "N", "n" and "0")
  3. {{yesno|blank= eggs}} result "eggs"
    1. but {{yesno|no=ham}} result "ham"
    2. and {{yesno|blank= eggs|no=ham}} result "eggs"
  4. {{yesno|¬|¬=sausage}} result "sausage"
  5. {{yesno|purplemonkeydishwasher|def=cup-of-tea}} result "cup-of-tea" (also applies to any other value not given above).
    1. but {{yesno|purplemonkeydishwasher|yes=bacon}} result "bacon"
    2. but {{yesno|purplemonkeydishwasher|def=cup-of-tea|yes=bacon}} result "cup-of-tea"

This may be used (apparently perversely) thus:

  • {{yesno|yes|yes=no|no=yes}} result "no"
  • {{yesno|no|yes=no|no=yes}} result "yes"

This creates a logical inversion.

See also


-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.

return function (val, default)
	-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
	-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
	-- following line.
	val = type(val) == 'string' and val:lower() or val
	if val == nil then
		return nil
	elseif val == true 
		or val == 'yes'
		or val == 'y'
		or val == 'true'
		or val == 't'
		or tonumber(val) == 1
	then
		return true
	elseif val == false
		or val == 'no'
		or val == 'n'
		or val == 'false'
		or val == 'f'
		or tonumber(val) == 0
	then
		return false
	else
		return default
	end
end