Module:Example
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Example/doc
--[[
All Lua modules on Wikivoyage must begin by defining a variable that will hold
their externally accessible functions. Such variables can have whatever name
you want and may also contain various data and/or functions.
--]]
local p = {};
-- Add a function to "p". Such functions are callable in Wikivoyage via the
-- #invoke command. "frame" will contain the data that Wikivoyage sends this
-- function when it runs. 'Hello' is a name of your choice; the same name needs
-- to be referred to when the module is used.
p.hello = function( frame )
local str = "Hello World!" -- Declare local variable set to "Hello World!".
return str -- This quits the function and sends the information in "str"
-- back to Wikivoyage.
end -- End of function "hello".
-- Add another function. To access arguments passed to a module, use
--`frame.args`, where `frame.args[1]` refers to the first unnamed parameter.
function p.hello_to(frame)
local name = frame.args[1]
-- `..` concatenates strings. This will return a customized greeting
-- depending on the name given, such as "Hello, Fred!"
return "Hello, " .. name .. "!"
end
function p.count_listings(frame)
-- Named arguments ({{#invoke:Example|count_listings|foo=bar}}) are likewise
-- accessed by indexing `frame.args` by name (`frame.args["buy"]`, or
-- equivalently, `frame.args.buy`.
local num_buy = tonumber(frame.args.buy) or 0
local num_drink = tonumber(frame.args.drink) or 0
-- Ternary operators assign values based on a condition in a compact way.
-- Here, `conj_buy` gets `'buy'` if `num_buy` is 1, else `'buys'`.
-- Similarly, `conj_drink` gets `'drink'` if `num_drink` is 1, else
-- `'drinks'`.
local conj_buy = num_buy == 1 and 'buy' or 'buys'
local conj_drink = num_drink == 1 and 'drink' or 'drinks'
-- Like above, concatenate a bunch of strings together to produce a sentence
-- based on the arguments given.
return 'I have ' .. num_buy .. ' ' .. conj_buy .. ' and ' .. num_drink .. ' ' .. conj_drink
end
-- One can define custom functions for use. Here we define a function 'lucky'
-- that has two inputs a and b. The names are of your choice.
local function lucky(a, b)
-- Condition: if b is the string 'yeah'. Strings require quotes. Remember to
-- include 'then'.
if b == 'yeah' then
-- Outputs 'a is my lucky number.' if the above condition is met.
return a .. ' is my lucky number.'
-- If no conditions are met, i.e. if b is anything else, output specified on
-- the next line. 'else' should not have 'then'.
else
-- Simply output a.
return a
-- The 'if' section should end with 'end'.
end
-- As should 'function'.
end
function p.Name2(frame)
-- The next five lines are mostly for convenience only and can be used as is
-- for your module.
-- This line allows template parameters to be used in this code easily. The
-- equal sign is used to define variables. 'pf' can be replaced with a word
-- of your choice.
local pf = frame:getParent().args
-- This line allows parameters from {{#invoke:}} to be used easily. 'f' can
-- be replaced with a word of your choice.
local f = frame.args
-- f[1] and pf[1], which we just defined, refer to the first parameter. This
-- line shortens them as 'M' for convenience. You could use the original
-- variable names.
local M = f[1] or pf[1]
-- Second shortened as 'm'.
local m = f[2] or pf[2]
-- A named parameter 'lucky' is shortend as l. Note that the syntax is
-- different from unnamed parameters.
local l = f.lucky or pf.lucky
-- If the second parameter is not used.
if m == nil then
-- Outputs the string 'Lonely' if the first condition is met.
return 'Lonely'
-- If condition 1 is not met, this line tests if M is greater than m.
elseif M > m then
-- If the condition is met, the difference is calculated and passed to
-- the self-defined function along with l. The output depends on whether
-- l is set to 'yeah'.
return lucky(M - m, l)
else
return 'Be positive!'
end
end
-- All modules end by returning the variable containing their functions to
-- Wikivoyage.
return p
--[[
Now we can use this module by calling:
{{#invoke:Example|hello}},
{{#invoke:Example|hello_to|foo}}, or
{{#invoke:Example|count_listings|buy=5|drink=6}}
Note that the first part of the invoke is the name of the Module's wikipage,
and the second part is the name of one of the functions attached to the
variable that you returned.
The "print" function is not allowed on Wikivoyage. All output is accomplished
via strings "returned" to Wikivoyage.
--]]