Module:Example
- Afrikaans
- العربية
- অসমীয়া
- تۆرکجه
- বাংলা
- Català
- Ελληνικά
- فارسی
- 한국어
- हिन्दी
- Bahasa Indonesia
- ಕನ್ನಡ
- Kurdî
- Ladin
- Latina
- Lietuvių
- მარგალური
- नेपाली
- 日本語
- Norsk bokmål
- ଓଡ଼ିଆ
- Oʻzbekcha / ўзбекча
- ਪੰਜਾਬੀ
- Português
- සිංහල
- Simple English
- Slovenščina
- کوردی
- Српски / srpski
- தமிழ்
- Taclḥit
- ไทย
- Українська
- Tiếng Việt
- 粵語
- 中文
- Kumoring
Appearance
From Wikipedia, the free encyclopedia
| Description | Module. |
|---|---|
| Code source | Example |
| Status | Unknown |
Example module.
This page is not a sandbox.
It should not be used for test editing. To experiment, please use the Wikipedia sandbox, your user sandbox, or other sandboxes.
This page serves as a reference for demonstrations and examples only, similar to other Namespace example pages. It is not part of the main encyclopedia.
This is example documentation for the module, used for demonstrations and the like.
Documentation
Package items
example.hello(frame)(function)- Hello world function
- Parameter:
framecurrent frame (table) - Returns: Hello world
example.hello_to(frame)(function)- Hello world function
- Parameters:
- Returns: Hello world
example.count_fruit(frame)(function)- Counts fruit
- Parameters:
- Returns: Number of apples and bananas
example.Name2(frame)(function)- Name2
- Parameter:
framecurrent frame (table) - Returns: Some output
See also
- Module:Sandbox: For practice/experimental/testing modules.
The above documentation is transcluded from Module:Example/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.
--- Example module. -- @module example -- @alias p localp={}--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. --- Hello world function -- @param {table} frame current frame -- @return Hello world 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. localstr="Hello World!"--Declare a local variable and set it equal to --"Hello World!". returnstr--This tells us to quit this function and send the information in --"str" back to Wikipedia. end-- end of the function "hello" --- Hello world function -- @param {table} frame current frame -- @param {string} frame.args[1] name -- @return Hello world functionp.hello_to(frame)-- Add another function localname=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 --- Counts fruit -- @param {table} frame current frame -- @param {string} frame.args.bananas number of bananas -- @param {string} frame.args.apples number of apples -- @return Number of apples and bananas functionp.count_fruit(frame) localnum_bananas=tonumber(frame.args.bananas)or0-- Named arguments ({{#invoke:Example|count_fruit|foo=bar}}) localnum_apples=tonumber(frame.args.apples)or0-- are likewise accessed by indexing `frame.args` by name (`frame.args["bananas"]`, -- or equivalently `frame.args.bananas`. localconj_bananas=num_bananas==1and'banana'or'bananas' localconj_apples=num_apples==1and'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 --- Lucky function -- @param {string} a -- @param {string} b -- @return Whether a is lucky. localfunctionlucky(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. ifb=='yeah'then-- Condition: if b is the string 'yeah'. Strings require quotes. Remember to include 'then'. returna..' is my lucky number.'-- Outputs 'a is my lucky number.' if the above condition is met. The string concatenation operator is denoted by 2 dots. else-- If no conditions are met, i.e. if b is anything else, output specified on the next line. 'else' should not have 'then'. returna-- Simply output a. end-- The 'if' section should end with 'end'. end-- As should 'function'. --- Name2 -- @param {table} frame current frame -- @return Some output functionp.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. localpf=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. localf=frame.args-- This line allows parameters from {{#invoke:}} to be used easily. 'f' can be replaced with a word of your choice. localM=f[1]orpf[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. localm=f[2]orpf[2]-- Second shortened as 'm'. locall=f.luckyorpf.lucky-- A named parameter 'lucky' is shortend as l. Note that the syntax is different from unnamed parameters. ifm==nilthen-- If the second parameter is not used. return'Lonely'-- Outputs the string 'Lonely' if the first condition is met. elseifM>mthen-- If the first condition is not met, this line tests a second condition: if M is greater than m. returnlucky(M-m,l)-- 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'. else return'Be positive!' end end returnp--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.