Lua/Frame Object

From Wikiversity
< Lua
Jump to navigation Jump to search

Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] This lesson will show you how to use the Lua Frame object in your scripts.

Prerequisites[edit | edit source]

This lesson assumes you have already completed the Tables lesson.

Create a Lua Script that Uses the Frame Object[edit | edit source]

To create a Lua script that uses the Frame object:

  1. Navigate to Module:Sandbox.
  2. Clear all existing code.
    It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
  3. Add the following code and save the page:
local p = {}

local function tableToString(t)
    local key
    local value
    local result
    result = ''
    for key, value in pairs(t) do
        if (tonumber(key) ~= nil) then
            result = result .. ':table[' .. key .. '] is ' .. value .. '\n' 
            result = result .. ':table[\'' .. key .. '\'] is ' .. value .. '\n' 
    return result
function p.args(frame)
    return ';args\n' .. tableToString(frame.args)

function p.callParserFunction(frame)
    return ';callParserFunction\n:' .. frame:callParserFunction('#time', 'Y-m-d H:i:s') .. '\n'

function p.expandTemplate(frame)
    return ';expandTemplate\n:' .. frame:expandTemplate({title = 'Template:Sandbox', args = {'arg1', 'arg2'}}) .. '\n'

function p.extensionTag(frame)
    return ';extensionTag\n:' .. frame:extensionTag('nowiki', '[[text]]', {}) .. '\n'

function p.getParent(frame)
	frame = frame:getParent()
    return ';getParent\n:' .. frame:getTitle() .. '\n'

function p.getTitle(frame)
    return ';getTitle\n:' .. frame:getTitle() .. '\n'

return p

Test Your Lua Script[edit | edit source]

To test your Lua script:

  1. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  2. Add the following code and save the page:
{{#invoke:Sandbox|args|These|are|arg3=frame args.}}

The result should be similar to:

table[1] is These
table[2] is are
table['arg3'] is frame args.
2013-12-20 02:20:13
Module talk:Sandbox

Understand Your Lua Script[edit | edit source]

To understand your Lua script:

  1. local function tableToString(t) declares a local function named tableToString that accepts a single parameter t, which is the table to be converted to a string.
    See Lua/Tables for more information.
  2. function p.args(frame) declares a function named args which accepts a single parameter frame, which is the interface to the calling object.
    • frame.args is a table of arguments passed to the function.
    • Positional arguments are accessed by numeric position.
    • Named arguments are accessed by name.
  3. frame:callParserFunction('#time', 'Y-m-d H:i:s') calls the parser function #time, passing the string literal 'Y-m-d H:i:s' to display the current time.
  4. frame:expandTemplate({title = 'Template:Sandbox', args = {'arg1', 'arg2'}}) calls the Template:Sandbox template, passing a table of parameter arguments and returns the result.
  5. frame:extensionTag('nowiki', '[[text]]', {}) tags the given text to control processing when the text is returned to the calling object.
  6. frame:getParent() returns the parent of the calling object, or in this case the page that invoked the module.
  7. frame:getTitle() returns a string containing the title of the frame.

Conclusion[edit | edit source]

Congratulations! You've now created, tested, and understood a Lua script that uses the Frame object. Return to the main Lua page to learn about other Lua code libraries.

See Also[edit | edit source]

References[edit | edit source]