This is an example module that demonstrates how to access arguments. It is used in Scribunto: An Introduction.

Hello world

sunting
{{#invoke:ArgsDemo|hello_world}}

This function displays the text "Hello, world!". It doesn't use any arguments.

Example:

  • {{#invoke:ArgsDemo|hello_world}} → Hello, world!
{{#invoke:ArgsDemo|hello|name}}

This function greets the specified user. It uses one argument, name.

Examples:

  • {{#invoke:ArgsDemo|hello|Fred}} → Hello, Fred!
  • {{#invoke:ArgsDemo|hello|Jane}} → Hello, Jane!
{{#invoke:ArgsDemo|add|num1|num2}}

This function adds two numbers, num1 and num2.

Examples:

  • {{#invoke:ArgsDemo|add|5|3}} → 8
  • {{#invoke:ArgsDemo|add|100|25}} → 125

Count fruit

sunting
{{#invoke:ArgsDemo|count|bananas=number|apples=number}}

This function displays how many apples and how many bananas we have. It uses named arguments.

Examples:

  • {{#invoke:ArgsDemo|count_fruit|bananas=5|apples=3}} → I have 5 bananas and 3 apples
  • {{#invoke:ArgsDemo|count_fruit|apples=7|bananas=4}} → I have 4 bananas and 7 apples

Has fruit

sunting
{{#invoke:ArgsDemo|has_fruit|name|bananas=number|apples=number|cherries=number}}

Displays the number of bananas, apples and cherries a named person has. This function mixes positional arguments with optional named arguments.

Examples:

  • {{#invoke:ArgsDemo|has_fruit|Fred|bananas=5|cherries=7}} → Fred has: 5 bananas 7 cherries
  • {{#invoke:ArgsDemo|has_fruit|Jane|cherries=9|apples=4}} → Jane has: 4 apples 9 cherries

Custom fruit

sunting
{{#invoke:ArgsDemo|custom_fruit|fruit1=number|fruit2=number|...}}

Displays the number of items of different fruit. Any fruit can be displayed; the function is not limited to particular types of fruit as it was in previous functions. This is done by iterating over the arguments. Note that the arguments are processed in an arbitrary order.

Examples:

  • {{#invoke:ArgsDemo|custom_fruit|pineapples=10|oranges=5}} → I have: 5 oranges 10 pineapples
  • {{#invoke:ArgsDemo|custom_fruit|lemons=6|grapefruits=3|bananas=4|grapes=20}} → I have: 4 bananas 6 lemons 3 grapefruits 20 grapes

Custom fruit 2

sunting
{{#invoke:ArgsDemo|custom_fruit_2|name|fruit1=number|fruit2=number|...}}

Displays the number of items of different fruit that a named person has. Similarly to the custom_fruit function, this function iterates over the arguments, and so is not limited to any particular types of fruit. The difference is that the function checks that each fruit is not the first positional parameter, so that name isn't erroneously listed as a fruit.

Examples:

  • {{#invoke:ArgsDemo|custom_fruit_2|Fred|pineapples=10|oranges=5}} → Fred has: 5 oranges 10 pineapples
  • {{#invoke:ArgsDemo|custom_fruit_2|Jane|lemons=6|grapefruits=3|bananas=4|grapes=20}} → Jane has: 4 bananas 6 lemons 3 grapefruits 20 grapes

See also

sunting

-- Sample Module demonstrating how to access arguments.
-- For more about the frame object, see http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object

local p = {}

-- No arguments, used like: {{#invoke:ArgsDemo|hello_world}}
function p.hello_world()
	return "Hello, world!"
end

-- One argument, used like: {{#invoke:ArgsDemo|hello|Fred}}
function p.hello(frame)
	local name = frame.args[1]
	return "Hello, " .. name .. "!"
end

-- Two arguments, used like: {{#invoke:ArgsDemo|add|5|3}}
function p.add(frame)
	local num1 = tonumber(frame.args[1])
	local num2 = tonumber(frame.args[2])
	return num1 + num2
end

-- Named arguments, used like: {{#invoke:ArgsDemo|count_fruit|bananas=5|apples=3}}
function p.count_fruit(frame)
	local num_bananas = frame.args.bananas
	local num_apples = frame.args.apples
	return 'I have ' .. num_bananas .. ' bananas and ' .. num_apples .. ' apples'
end

-- Mixing regular args with named args and optional named args
-- Used like: {{#invoke:ArgsDemo|has_fruit|Fred|bananas=5|cherries=7}}
function p.has_fruit(frame)
	local name = frame.args[1]
	local num_bananas = frame.args.bananas
	local num_apples = frame.args.apples
	local num_cherries = frame.args.cherries

	local result = name .. ' has:'
	if num_bananas then result = result .. ' ' .. num_bananas .. ' bananas' end
	if num_apples then result = result .. ' ' .. num_apples .. ' apples' end
	if num_cherries then result = result .. ' ' .. num_cherries .. ' cherries' end
	return result
end

-- Iterating over args, used like: {{#invoke:ArgsDemo|custom_fruit|pineapples=10|kiwis=5}}
function p.custom_fruit(frame)
	local result = 'I have:'
	for name, value in pairs(frame.args) do
		result = result .. ' ' .. value .. ' ' .. name
	end
	return result
end

-- Iterating over args with separate mandatory args
-- Used like: {{#invoke:ArgsDemo|custom_fruit_2|Fred|pineapples=10|kiwis=5}}
function p.custom_fruit_2(frame)
	local name = frame.args[1]
	local result = name .. ' has:'
	for name, value in pairs(frame.args) do
		if name ~= 1 then
			result = result .. ' ' .. value .. ' ' .. name
		end
	end
	return result
end

return p