Mòideal:Example
(deasbaireachd⧼tpt-languages-separator⧽ ⧼tpt-languages-separator⧽eachdraidh⧼tpt-languages-separator⧽ceanglaichean⧼tpt-languages-separator⧽doc⧼tpt-languages-separator⧽bogsa-gainmhich⧼tpt-languages-separator⧽cùisean deuchainn)
This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This page, like the others here, exists for referencing by demonstrations, examples, etc. It does not form part of the encyclopedia. |
This is example documentation for the module, used for demonstrations and the like.
Faic cuideachd - Also see
- Module:Sandbox: For practice/experimental/testing modules.
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 "my_object".
--Such functions are callable in Wikipedia
--via the #invoke command.
--"frame" will contain the data that Wikipedia
--sends this function when it runs.
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!"
end
function p.count_fruit(frame)
local num_bananas = frame.args.bananas -- Named arguments ({{#invoke:Example|count_fruit|foo=bar}}) are likewise
local num_apples = frame.args.apples -- accessed by indexing `frame.args` by name (`frame.args["bananas"]`, or)
-- equivalently `frame.args.bananas`.
return 'I have ' .. num_bananas .. ' bananas and ' .. num_apples .. ' apples'
-- Like above, concatenate a bunch of strings together to produce
-- a sentence based on the arguments given.
end
return p --All modules end by returning the variable containing their functions to Wikipedia.
-- Now we can use this module by calling {{#invoke: Example | hello }},
-- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=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 in Wikipedia. All output is accomplished
-- via strings "returned" to Wikipedia.