Module:Template wrapper and Module:Template wrapper/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
Replace Module:No globals with require( "strict" ) |
use require('strict') instead of require('Module:No globals') |
||
Line 82: | Line 82: | ||
local function frame_args_get (frame_args, args, list) |
local function frame_args_get (frame_args, args, list) |
||
local template; |
local template; |
||
local module_; |
|||
local function_; |
|||
for k, v in pairs (frame_args) do -- here we get the wrapper template's 'default' parameters |
for k, v in pairs (frame_args) do -- here we get the wrapper template's 'default' parameters |
||
Line 87: | Line 89: | ||
if '_template' == k then |
if '_template' == k then |
||
template = v; -- save the name of template that we are wrapping |
template = v; -- save the name of template that we are wrapping |
||
elseif '_module' == k then |
|||
module_ = v; -- save the name of template that we are wrapping |
|||
elseif '_function' == k then |
|||
function_ = v; |
|||
elseif '_exclude' ~= k and '_reuse' ~= k and '_include-positional' ~= k and '_alias-map' ~= k then -- these already handled so ignore here; |
elseif '_exclude' ~= k and '_reuse' ~= k and '_include-positional' ~= k and '_alias-map' ~= k then -- these already handled so ignore here; |
||
add_parameter (k, v, args, list); -- add all other parameters to args in the style dictated by list |
add_parameter (k, v, args, list); -- add all other parameters to args in the style dictated by list |
||
Line 93: | Line 99: | ||
end |
end |
||
if module_ == nil and function_ == nil then |
|||
return template; -- return contents of |_template= parameter |
|||
return template; |
|||
elseif module_ ~= nil and function_ ~= nil then |
|||
return module_, function_; |
|||
end |
|||
end |
end |
||
Line 139: | Line 149: | ||
local function _main (frame, args, list) |
local function _main (frame, args, list) |
||
local template; |
local template; |
||
local fn_name; |
|||
local exclude = {}; -- table of parameter names for parameters that are not passed to the working template |
local exclude = {}; -- table of parameter names for parameters that are not passed to the working template |
||
local reuse_list = {}; -- table of pframe parameter names whose values are modified before they are passed to the working template as the same name |
local reuse_list = {}; -- table of pframe parameter names whose values are modified before they are passed to the working template as the same name |
||
Line 156: | Line 167: | ||
end |
end |
||
template = frame_args_get (frame.args, args, list); |
template, fn_name = frame_args_get (frame.args, args, list); -- get parameters provided in the {{#invoke:template wrapper|...|...}} |
||
if nil == template or '' == template then -- this is the one parameter that is required by this module |
if nil == template or '' == template then -- this is the one parameter that is required by this module |
||
return nil; -- not present, tell calling function to emit an error message |
return nil; -- not present, tell calling function to emit an error message |
||
Line 215: | Line 226: | ||
pframe_args_get (pframe_args, args, exclude, _include_positional, list); -- add parameters and values to args that are not listed in the exclude table |
pframe_args_get (pframe_args, args, exclude, _include_positional, list); -- add parameters and values to args that are not listed in the exclude table |
||
return template; -- args now has all default and live parameters, return working template name |
return template, fn_name; -- args now has all default and live parameters, return working template name |
||
end |
end |
||
Line 228: | Line 239: | ||
local args = {}; -- table of default and live parameters and their values to be passed to the wrapped template |
local args = {}; -- table of default and live parameters and their values to be passed to the wrapped template |
||
local template; -- the name of the working template |
local template; -- the name of the working template |
||
local fn_name; |
|||
template = _main (frame, args, false); |
template, fn_name = _main (frame, args, false); -- get default and live parameters and the name of the working template |
||
if not template then -- template name is required |
if not template then -- template name is required |
||
return error_msg; -- emit error message and abandon if template name not present |
return error_msg; -- emit error message and abandon if template name not present |
||
end |
end |
||
if fn_name == nil then |
|||
return frame:expandTemplate {title=template, args=args}; -- render the working template |
|||
else |
|||
local newFrame = { |
|||
getParent = function(self) |
|||
return frame |
|||
end, |
|||
args = args, |
|||
}; |
|||
setmetatable(newFrame, { |
|||
__index = function(t, k) |
|||
if type(frame[k]) == 'function' then |
|||
return function(...) |
|||
return frame[k](frame, select(2, ...)) |
|||
end |
|||
else |
|||
return frame[k] |
|||
end |
|||
end |
|||
}) |
|||
return require('Module:' .. template)[fn_name](newFrame) |
|||
end |
|||
end |
end |
||