Module:Template wrapper: Difference between revisions
From Vigyanwiki
Template>Trappist the monk (ignore empty or positional default parameters;) |
Template>Trappist the monk m (Reverted edits by Trappist the monk (talk) to last version by EdJohnston) |
||
Line 2: | Line 2: | ||
local p={}; | local p={}; | ||
local function isInTable(tbl, item) | |||
for key, value in pairs(tbl) do | |||
if value == item then return true end | |||
local function | |||
for | |||
if | |||
end | end | ||
return false | return false | ||
end | end | ||
function p.wrap (frame) | |||
local args = {}; | |||
local template; | local template; | ||
local exclude = {}; | |||
for k, v in pairs ( | |||
if ' | for k, v in pairs (frame.args) do -- here we get the wrapper template's 'default' parameters | ||
if '_template' == k then | |||
template = v; -- save the name of template that we are wrapping | |||
elseif '_exclude' == k then | |||
exclude = mw.text.split(v, "%s*,%s*"); -- save the excluded parameters | |||
else | |||
args[k] = v; -- copy frame parameters to args table | |||
end | end | ||
end | |||
if nil == template or '' == template then -- this is the one parameter required by this module | |||
return '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">|template=</code> missing or empty</span>'; | |||
end | end | ||
local pframe = frame:getParent(); -- here we get the wrapper template's 'live' parameters | |||
for k, v in pairs (pframe.args) do | |||
if 'string' == type (k) and not isInTable(exclude, k) then -- do not pass along positional or excluded parameters | |||
if v and v ~= '' then -- pass along only those parameters that have assigned values | |||
for k, v in pairs ( | |||
if 'string' == type (k) and not | |||
if v and | |||
if 'unset' == v:lower() then -- special keyword to unset 'default' parameters set in the wrapper template | if 'unset' == v:lower() then -- special keyword to unset 'default' parameters set in the wrapper template | ||
v = ''; -- unset the value | v = ''; -- unset the value | ||
end | end | ||
args[k] = v; -- copy parent frame parameters to args table | |||
end | end | ||
end | end | ||
end | end | ||
return frame:expandTemplate {title=template, args=args}; -- render the template | return frame:expandTemplate {title=template, args=args}; -- render the template | ||
end | end | ||
return p; | return p; |
Revision as of 04:28, 10 June 2018
Documentation for this module may be created at Module:Template wrapper/doc
require('Module:No globals');
local p={};
local function isInTable(tbl, item)
for key, value in pairs(tbl) do
if value == item then return true end
end
return false
end
function p.wrap (frame)
local args = {};
local template;
local exclude = {};
for k, v in pairs (frame.args) do -- here we get the wrapper template's 'default' parameters
if '_template' == k then
template = v; -- save the name of template that we are wrapping
elseif '_exclude' == k then
exclude = mw.text.split(v, "%s*,%s*"); -- save the excluded parameters
else
args[k] = v; -- copy frame parameters to args table
end
end
if nil == template or '' == template then -- this is the one parameter required by this module
return '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">|template=</code> missing or empty</span>';
end
local pframe = frame:getParent(); -- here we get the wrapper template's 'live' parameters
for k, v in pairs (pframe.args) do
if 'string' == type (k) and not isInTable(exclude, k) then -- do not pass along positional or excluded parameters
if v and v ~= '' then -- pass along only those parameters that have assigned values
if 'unset' == v:lower() then -- special keyword to unset 'default' parameters set in the wrapper template
v = ''; -- unset the value
end
args[k] = v; -- copy parent frame parameters to args table
end
end
end
return frame:expandTemplate {title=template, args=args}; -- render the template
end
return p;