Module:Transclude
From Neodyland Wiki
More actions
This documentation is transcluded from Module:Transclude/doc.
-- <nowiki>
--------------------------------------------------------------------------------
-- Module that allows transcluding a template with all parent arguments.
-- Useful in `/i18n` template subpages that delegate to a `/layout` subpage.
--
-- @module transclude
-- @author [[User:ExE Boss]]
-- @require [[Module:No globals]]
--------------------------------------------------------------------------------
require('Module:No globals')
local checkType = require('libraryUtil').checkType;
local INVALID_TITLES = {
["."] = true,
[".."] = true,
};
--------------------------------------------------------------------------------
-- Helper function that merges argument tables.
--
-- @function mergeArgs
-- @param {table} inArgs
-- @param {table} outArgs
--------------------------------------------------------------------------------
local function mergeArgs(inArgs, outArgs)
checkType("mergeArgs", 1, inArgs, "table");
checkType("mergeArgs", 2, outArgs, "table");
for name, value in pairs(inArgs) do
outArgs[name] = value;
end
return outArgs;
end
local mt = {};
function mt.__index(_, name)
if (
type(name) ~= "string"
or INVALID_TITLES[name]
or mw.ustring.find(name, "#", nil, true)
) then
return nil;
end
local title = mw.title.new(name, "Template")
if (not title) then
return nil;
end
return function (frame)
checkType('transclude["' .. name .. '"]', 1, frame, "table");
local args = {};
mergeArgs(frame:getParent().args, args);
mergeArgs(frame.args, args);
if (not title.exists) then
return "[[:" .. title.fullText .. "]]";
end
return frame:expandTemplate{
title = title,
args = args,
};
end
end
return setmetatable({}, mt);