Module:Sandbox/Artoria2e5/Fallback/doc

< Module:Sandbox‎ | Artoria2e5/Fallback
Revision as of 21:24, 7 Mayıs 2022 by imported>Qwerfjkl (bot) (Replaced deprecated <source> tags with <syntaxhighlight> (via WP:JWB))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is the documentation page for Module:Sandbox/Artoria2e5/Fallback


UsageEdit

This module provides a function which adds fallback functionalities to a given table args according to the fallback sequence given in arg_aliases. Note that this module currently only supports indexing, i.e. __index.

ParametersEdit

args
The table to add fallback functionalities.
arg_aliases
A table of the form { [key] = { fallback1, fallback2... } }.

ExampleEdit

The following snippet adds template parameter aliases to a metatable obtained from Module:Arguments.

local getArgs = require('Module:Arguments').getArgs
local makeFallback = require('Module:Sandbox/Artoria2e5/Fallback')
local p = {}

function p.main(frame)
    local args = makeFallback(getArgs(frame), {
        ["foo_bar"] = {"foobar", "foo", "bar"}
    })
    -- Try calling this template with |foo=42.
    return (args["foo_bar"] or "nothing here!")
end

return p

The following snippet shows basic usage of indexing fallback.

local makeFallback = require('Module:Arguments/Fallback')
table = {
    ["bar"] = "crunchy"
}

makeFallback(table, {
    ["foo_bar"] = {"foobar", "foo", "bar"}
})

return table["foo_bar"]

The following snippet creates a fallback list with all fallback names aliasing back to the main name.

local makeFallback = require('Module:Sandbox/Artoria2e5/Fallback')
table = {
    ["bar"] = "crunchy"
}
makeFallback(table, {
    ["foo_bar"] = {"foobar", "foo", "bar"},
    ["foo"]     = {"foo_bar"}, 
    ["foobar"]  = {"foo_bar"},
    ["bar"]     = {"foo_bar"}
})
return table["foo"]