Module:families/templates

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:families/templates/documentation. [edit]
Useful links: root pageroot page's subpagestransclusionstestcases

This module provides access to Module:families from templates, so that they can make use of the information stored there.

Exported functions[edit]

exists[edit]

{{#invoke:families/templates|exists|(family code)}}

Check whether a family code exists and is valid. It will return "1" if the family code exists, and the empty string "" if it does not.

This is rarely needed, because a script error will result when someone uses a code that is not valid, so you do not need this just to check for errors. However, in case you need to decide different actions based on whether a certain parameter is a family code or something else, this function can be useful.

getByCode[edit]

{{#invoke:families/templates|getByCode|(family code)|(item to look up)|(index)}}

Queries information about a family code.

  • The family code should be one of the codes that is defined in Module:families data. If it is missing or does not exist, the result will be a script error.
  • The item is the name of one of the pieces of data that is stored for a family, such as names or family. If no item has been provided, the result will be a script error. However, if the name of a nonexistent item is given, the result will be an empty string. This allows you to check for the presence or absence of an item without triggering errors.
  • The index is optional, and is used for items that are lists, such as names. It selects which item in the list to return. On items that are single strings, like family, it has no effect. If no index is given, the default will be 1 (the first subitem). If an index is given that is higher than the number of items in the list, the result will be an empty string.

For example, to request the default (canonical) name of the family whose code is sla:

{{#invoke:families/templates|getByCode|sla|getCanonicalName}}
  • Result: Slavic

To request its second name, if any:

{{#invoke:families/templates|getByCode|sla|getAllNames|2}}
  • Result: Slavonic

To request its family:

{{#invoke:families/templates|getByCode|sla|getFamily}}
  • Result: ine-bsl

See also[edit]


local export = {}
 
function export.exists(frame)
	local args = frame.args
	local fam = args[1] or error("Family code has not been specified. Please pass parameter 1 to the module invocation.")
 
	fam = require("Module:families").getByCode(fam)
 
	if fam then
		return "1"
	else
		return ""
	end
end
 
function export.getByCode(frame)
	local args = frame.args
	local fam = args[1] or error("Family code (parameter 1) has not been specified.")
	local itemname = args[2] or error("Function to call (parameter 2) has not been specified.")
 
	fam = require("Module:families").getByCode(fam) or error("The family code '" .. fam .. "' is not valid.")
 
	-- The item that the caller wanted to look up
	if itemname == "getCanonicalName" then
		return fam:getCanonicalName()
	elseif itemname == "getAllNames" then
		local index = args[3]; if index == "" then index = nil end
		index = tonumber(index or error("Numeric index of the desired item in the list (parameter 3) has not been specified."))
		return fam:getAllNames()[index] or ""
	elseif itemname == "getFamily" then
		return fam:getFamily():getCode()
	elseif itemname == "getCategoryName" then
		return fam:getCategoryName()
	else
		error("Requested invalid item name \"" .. itemname .. "\".")
	end
end
 
function export.getByCanonicalName(frame)
	local args = frame.args
	local famname = args[1] or error("Family name has not been specified. Please pass parameter 1 to the module invocation.")
 
	local fam = require("Module:families").getByCanonicalName(famname)
 
	if fam then
		return fam:getCode()
	else
		return ""
	end
end
 
return export