Module:Example/sandbox
Appearance
From Wikipedia, the free encyclopedia
This is the module sandbox page for Module:Example (diff).
See also the companion subpage for test cases (run).
See also the companion subpage for test cases (run).
| 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.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
-------------------------------------------------------------------------------- -- Module:Example -- Demonstration of a Lua Module for Wikipedia -- -- This module serves as an example and guide for creating Lua modules on -- Wikipedia. It defines several functions that can be invoked via the -- {{#invoke:}} mechanism. Note that this module is for demonstration purposes -- only and is not intended for actual production use. -------------------------------------------------------------------------------- localp={}-- Table to hold the module's externally accessible functions. -------------------------------------------------------------------------------- -- Function: p.hello -- Description: Returns a simple, fixed greeting "Hello World!". -- Usage: {{#invoke:Example|hello}} -------------------------------------------------------------------------------- p.hello=function(frame) localgreeting="Hello World!"-- Define the greeting message as a local variable. returngreeting-- Return the greeting to Wikipedia. end -------------------------------------------------------------------------------- -- Function: p.hello_to -- Description: Returns a personalized greeting using the first unnamed parameter. -- Usage: {{#invoke:Example|hello_to|YourName}} -------------------------------------------------------------------------------- functionp.hello_to(frame) localname=frame.args[1]-- Retrieve the first unnamed parameter. return"Hello, "..name.."!"-- Concatenate and return the personalized greeting. end -------------------------------------------------------------------------------- -- Function: p.count_fruit -- Description: Constructs and returns a sentence indicating the count of bananas -- and apples, using proper singular/plural forms. -- Usage: {{#invoke:Example|count_fruit|bananas=5|apples=6}} -------------------------------------------------------------------------------- functionp.count_fruit(frame) -- Convert the named parameters to numbers; default to 0 if conversion fails. localnum_bananas=tonumber(frame.args.bananas)or0 localnum_apples=tonumber(frame.args.apples)or0 -- Determine the correct word for singular or plural form. localbanana_label=(num_bananas==1)and"banana"or"bananas" localapple_label=(num_apples==1)and"apple"or"apples" -- Construct and return the complete sentence. return"I have "..num_bananas.." "..banana_label.. " and "..num_apples.." "..apple_label.."." end -------------------------------------------------------------------------------- -- Local Helper Function: lucky -- Description: Returns a message stating that the given number is "lucky" if the -- second parameter is the string "yeah"; otherwise, it simply returns -- the number. -------------------------------------------------------------------------------- localfunctionlucky(a,b) ifb=="yeah"then returna.." is my lucky number." else returna end end -------------------------------------------------------------------------------- -- Function: p.Name2 -- Description: Demonstrates the use of both unnamed and named parameters from the -- frame object. It accesses parameters from the current frame as well as -- from the parent frame, and returns a message based on the provided values. -- Usage: Can be invoked with parameters directly or via a parent template. -------------------------------------------------------------------------------- functionp.Name2(frame) -- Retrieve parameters from both the parent frame and the current frame. -- The parent frame allows template parameters to be used in this code easily. localparentArgs=frame:getParent().args localargs=frame.args -- Use the first and second unnamed parameters, with a fallback to parent arguments. localM=args[1]orparentArgs[1] localm=args[2]orparentArgs[2] -- Retrieve the named parameter 'lucky' (if provided). localluckyParam=args.luckyorparentArgs.lucky -- Determine the output based on the provided parameters. ifm==nilthen return"Lonely"-- If the second parameter is missing, return "Lonely". elseifM>mthen -- If M is greater than m, calculate the difference and use the lucky helper. returnlucky(M-m,luckyParam) else return"Be positive!" end end -------------------------------------------------------------------------------- -- Return the module table to make the functions accessible via {{#invoke:}}. -------------------------------------------------------------------------------- returnpositive