local p = ; --All Lua modules on Wikipedia 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 as well as functions.p.hello = function(frame) --Add a function to "p". --Such functions are callable in Wikipedia --via the #invoke command. --"frame" will contain the data that Wikipedia --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. local str = "Hello World!" --Declare a local variable and set it equal to --"Hello World!". return str --This tells us to quit this function and send the information in --"str" back to Wikipedia. end -- end of the function "hello"function p.hello_to(frame) -- Add another function local name = frame.args[1] -- To access arguments passed to a module, use `frame.args` -- `frame.args[1]` refers to the first unnamed parameter -- given to the module return "Hello, " .. name .. "!" -- `..` concatenates strings. This will return a customized -- greeting depending on the name given, such as "Hello, Fred!"endfunction p.count_fruit(frame) local num_bananas = tonumber(frame.args.bananas) or 0 -- Named arguments local num_apples = tonumber(frame.args.apples) or 0 -- are likewise accessed by indexing `frame.args` by name (`frame.args["bananas"]`, -- or equivalently `frame.args.bananas`. local conj_bananas = num_bananas
1 and 'apple' or 'apples' -- Ternary operators assign values based on a condition in a compact way. -- Here, `conj_bananas` gets `'banana'` if `num_bananas` is 1, else `'bananas'`. -- Similarly, `conj_apples` gets `'apple'` if `num_apples` is 1, else `'apples'`. return 'I have ' .. num_bananas .. ' ' .. conj_bananas .. ' and ' .. num_apples .. ' ' .. conj_apples -- Like above, concatenate a bunch of strings together to produce -- a sentence based on the arguments given.end
local function lucky(a, b) -- 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. if b
function p.Name2(frame) -- The next five lines are mostly for convenience only and can be used as is for your module. The output conditions start on line 50. local pf = frame:getParent.args -- 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 f = frame.args -- This line allows parameters from to be used easily. 'f' can be replaced with a word of your choice. local M = f[1] or pf[1] -- 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[2] or pf[2] -- Second shortened as 'm'. local l = f.lucky or pf.lucky -- A named parameter 'lucky' is shortend as l. Note that the syntax is different from unnamed parameters. if m
return p --All modules end by returning the variable containing their functions to Wikipedia.-- Now we can use this module by calling,--, or -- 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 in Wikipedia. All output is accomplished-- via strings "returned" to Wikipedia.