模組:Template wrapper
![]() | 此模組被引用於約76,000個頁面。 為了避免造成大規模的影響,所有對此模組的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
本模組用於將模板封裝,以提供預設參數值,並允許編者向底層工作模板傳遞額外參數。
在編寫封裝模板時,應為此模組提供使用封裝模板所需的所有預設參數。然後,編者可以直接使用封裝模板,也可以提供其他封裝和規範參數。工作模板支持的所有規範參數都可以添加到封裝模板中,或由編者中條目中提供。當編者提供的參數在封裝模板中有預設值時,編者提供的值會覆蓋預設值。如果需要移除預設參數,編者可以將該參數值設定為關鍵字unset
。參數留空時本模組會將其丟棄。
匿名參數通常不會傳遞給工作模板。設定|_include-positional=yes
時將所有匿名參數傳遞給工作模板。匿名參數不能被排除;匿名參數也可以設為unset
。
僅由封裝模板使用的參數,要麼是匿名參數({{{n}}}),要麼在|_exclude=
中列出。本模組不會將_excluded
參數傳遞給工作模板。
用法
{{#invoke:Template wrapper|wrap|_template=working template|_exclude=named parameter, named parameter, ...|_reuse=named parameter, named parameter, ...|_alias-map=alias parameter:canonical parameter|_include-positional=yes|<default parameter>|<default parameter>|...}}
- 控制參數(詳見下方說明)
|_template=
– (必須)工作模板(即被封裝的模板)的名稱(不帶「Template:」命名空間前綴)|_exclude=
– 僅在封裝模板中使用、不傳遞給工作模板的參數列表,以逗號分隔|_reuse=
– 復用參數列表,以逗號分隔,這些參數由封裝模板和工作模板共用|_alias-map=
– 封裝參數至規範參數的映射列表,使用逗號分隔,作用是指定規範參數的別名|_include-positional=
– 填寫yes
時將所有匿名參數傳遞給工作模板
- 定義
- 規範參數(canonical parameter):工作模板支持和使用的參數
- 封裝參數(wrapper parameter):封裝模板使用的參數;可為規範參數提供數據,或用於控制封裝模板
- 別名參數(alias parameter):對封裝模板具有上下文意義的封裝參數,但必須重命名為規範參數以供工作模板使用
- 復用參數(reused parameter):由封裝模板和工作模板共用的參數,傳入工作模板時已被封裝模板修改
- 預設參數(default parameter)在封裝模板中給出預設值的規範參數
封裝模板 (wrapper template) |
Module:Template wrapper | 工作模板 (working template) | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|規範參數= |
→ | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | 過濾 排除參數 |
working template | |
|封裝參數= |
→ | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | |||
|_exclude= |
→ | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | → | ||||
|_include-positional= |
→ | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | |||||
|_alias-map= |
→ | 轉換別名參數 為規範參數 |
→ | |規範參數= |
→ | –––––––→ | → | –––––––→ | → | → | ||||
→ | → | 修改復用的 規範參數 | ||||||||||||
|別名參數= |
→ | –––––––→ | → | → | |復用參數= |
→ | –––→ | → | ||||||
|_reuse= |
→ | –––––––→ | → | –––––––→ | → | |||||||||
|規範參數= |
→ | –––––––→ | → | –––––––→ | → | –––––––→ | → | |||||||
|預設參數= |
→ | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––––––→ | → | –––→ | → |
參數
_template
唯一必需的參數,|_template=
提供工作模板(被封裝的模板)的名稱(不帶「Template:」命名空間前綴)。
_alias-map
|_alias-map=
封裝參數至規範參數的映射列表,使用逗號分隔,作用是指定工作模板規範參數在封裝模板中的別名。每項映射格式如下:
<from>:<to>
– 其中<from>
是封裝模板的參數名,<to>
是規範參數名
例如封裝模板中要使用|assessor=
參數,在工作模板中沒有|assessor=
參數,但有等效的|author=
參數,這時可寫為:
|_alias-map=assessor:author
匿名參數也可以映射為規範參數:
|_alias-map=1:author, 2:title, 3:language
可以使用#
枚舉符將封裝參數枚舉映射至規範參數:
|_alias-map=assessor#:author#
多個封裝參數可以映射到一個規範參數:
|_alias-map=1:author, assessor:author
|alias-map=
中列出的封裝參數不會傳遞給工作模板。設定|_include-positional=yes
時映射匿名參數可能導致不良後果。同時設定|_alias-map=1:author
和|_include-positional=yes
時,封裝模板中的{{{1}}}
傳入工作模板的|author=
,其他匿名參數也會傳入工作模板,即封裝模板的{{{2}}}
傳遞為工作模板的{{{2}}}
等等。
_reuse
|_reuse=
規範參數列表,使用逗號分隔,這些參數對封裝模板和工作模板都有意義。
在最簡單的情況下,傳入封裝模板的規範參數會覆蓋封裝模板中提供的預設參數。有時,一個封裝參數與規範參數的名稱相同,並且需要在封裝模板中對參數值修改後再傳入給工作模板。例如,|title=
既是封裝參數,也是規範參數,封裝模板需要將其修改後再傳入工作模板。為此,我們首先編寫:
|_reuse=title
之後,在封裝模板的{{#invoke:Template wrapper|wrap|_template=...|...}}
中編寫:
|title=Modified {{{title}}}
復用參數不能被覆蓋。
_exclude
|_exclude=
僅在封裝模板中使用、不傳遞給工作模板的參數列表,以逗號分隔。此列表適用於從封裝模板接收的所有封裝參數和規範參數(包括已重命名為別名參數的規範參數)。
例如,封住模板使用|id=
參數的值作為預設參數|url=
的部分內容,可以這樣寫:
|_exclude=id
之後,在封裝模板的{{#invoke:Template wrapper|wrap|_template=...|...}}
中編寫:
|url=https://example.com/{{{id}}}
這樣,被修改後的|url=
參數值被傳遞給工作模板,但|id=
參數不傳遞。
復用參數和預設參數不應被排除。
_include-positional
|_include-positional=
填寫yes
時將所有匿名參數傳遞給工作模板,預設狀態(留空/取消該參數)為排除匿名參數。
覆蓋預設參數
編者只需在封裝模板中將預設偏好設定為所需值,即可覆蓋預設參數。參數值留空時將被忽略,若要將預設參數設為空值,請填寫關鍵字unset
,以將該預設參數將作為空(無賦值)參數傳遞給工作模板。
復用參數不能設為unset
或被覆蓋。
調試/文檔模式
This module has two entry points. A wrapper template might use a module {{#invoke:}}
written like this:
{{#invoke:Template wrapper|{{#if:{{{_debug|}}}|list|wrap}}|_template=<working template>|_exclude=_debug, ...|...}}
where the |_debug=
wrapper parameter, set to any value, will cause the module to render the call to the working template without actually calling the working template.
As an example, {{cite wikisource}}
is a wrapper template that uses {{citation}}
as its working template. {{cite wikisource}}
accepts positional parameters but {{citation}}
does not so the wrapper template must convert the positional parameters to named parameters which it does using the |_alias-map=
parameter:
{{#invoke:template wrapper|{{#if:{{{_debug|}}}|list|wrap}}|_template=citation |_exclude=..., _debug <!-- unnecessary detail omitted --> |_alias-map=1:title, 2:author, 3:language
This example uses positional parameters and sets |_debug=yes
to show that the {{citation}}
template is correctly formed:
{{cite wikisource|Sentido y sensibilidad|Jane Austen|es|_debug=yes}}
- Jane Austen.
Sentido y sensibilidad. 維基文庫 (西班牙文).
- Jane Austen.
and, with |_debug=
unset:
{{cite wikisource|Sentido y sensibilidad|Jane Austen|es|_debug=}}
- Jane Austen.
Sentido y sensibilidad. 維基文庫 (西班牙文).
- Jane Austen.
The |_debug=
name is chosen here for convenience but may be anything so long as it matches the {{#if:}}
in the {{#invoke:}}
.
You may also call the link
function to get something like the left-hand side of Template:yy. This is essentially the list
function with the template name turned into a link.
Template:Yytop
Template:Yy
Template:Yybottom
require('strict');
local error_msg = '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">|_template=</code> missing or empty</span>';
--[[--------------------------< I S _ I N _ T A B L E >--------------------------------------------------------
scan through tbl looking for value; return true if found, false else
]]
local function is_in_table (tbl, value)
for k, v in pairs (tbl) do
if v == value then return true end
end
return false;
end
--[[--------------------------< A D D _ P A R A M E T E R >----------------------------------------------------
adds parameter name and its value to args table according to the state of boolean list argument; kv pair for
template execution; k=v string for template listing.
]]
local function add_parameter (k, v, args, list)
if list then
table.insert( args, table.concat ({k, '=', v})); -- write parameter names and values to args table as string
else
args[k] = v; -- copy parameters to args table
end
end
--[[--------------------------< A L I A S _ M A P _ G E T >----------------------------------------------------
returns a table of local template (parent frame) parameter names and the target template names that match where
in [key]=<value> pairs where:
[key] is local template parameter name (an alias)
<value> is target template parameter name (the canonical parameter name used in the working template)
The parameter |_alias-map= has the form:
|_alias-map=<list>
where <list> is a comma-separated list of alias / canonical parameter name pairs in the form
<from> : <to>
where:
<from> is the local template's parameter name (alias)
<to> is the target template's parameter name (canonical)
for enumerated parameters place an octothorp (#) where the enumerator digits are placed in the parameter names:
<from#> : <to#>
]]
local function alias_map_get (_alias_map)
local T = mw.text.split (_alias_map, '%s*,%s*'); -- convert the comma-separated list into a table of alias pairs
local mapped_aliases = {}; -- mapped aliases will go here
local l_name, t_name; -- parameter names
for _, alias_pair in ipairs (T) do -- loop through the table of alias pairs
l_name, t_name = alias_pair:match ('(.-)%s*:%s*(.+)'); -- from each pair, get local and target parameter names
if l_name and t_name then -- if both are set
if tonumber (l_name) then
l_name = tonumber (l_name); -- convert number-as-text to a number
end
mapped_aliases[l_name] = t_name; -- add them to the map table
end
end
return mapped_aliases;
end
--[[--------------------------< F R A M E _ A R G S _ G E T >--------------------------------------------------
Fetch the wrapper template's 'default' and control parameters; adds default parameters to args
returns content of |_template= parameter (name of the working template); nil else
]]
local function frame_args_get (frame_args, args, list)
local template;
for k, v in pairs (frame_args) do -- here we get the wrapper template's 'default' parameters
if 'string' == type (k) and (v and ('' ~= v)) then -- do not pass along positional or empty parameters
if '_template' == k then
template = v; -- save the name of template that we are wrapping
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
end
end
end
return template; -- return contents of |_template= parameter
end
--[=[--------------------------< P F R A M E _ A R G S _ G E T >------------------------------------------------
Fetches the wrapper template's 'live' parameters; adds live parameters that aren't members of the exclude table to
args table; positional parameters may not be excluded
no return value
]=]
local function pframe_args_get (pframe_args, args, exclude, _include_positional, list)
for k, v in pairs (pframe_args) do
if 'string' == type (k) and not is_in_table (exclude, k) then -- do not pass along 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 in the args table
end
add_parameter (k, v, args, list) -- add all other parameters to args in the style dictated by list; alias map only supported for local-template parameters
end
end
end
if _include_positional then
for i, v in ipairs (pframe_args) do -- pass along positional parameters
if 'unset' == v:lower() then -- special keyword to unset 'default' parameters set in the wrapper template
v = ''; -- unset the value in the args table
end
add_parameter (i, v, args, list);
end
end
end
--[[--------------------------< _ M A I N >--------------------------------------------------------------------
Collect the various default and live parameters into args styled according to boolean list.
returns name of the working or listed template or nil for an error message
]]
local function _main (frame, args, list)
local 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 alias_map = {}; -- table that maps parameter aliases to working template canonical parameter names
local _include_positional;
if frame.args._exclude and ('' ~= frame.args._exclude) then -- if there is |_exclude= and it's not empty
exclude = mw.text.split (frame.args._exclude, "%s*,%s*"); -- make a table from its contents
end
-- TODO: |_reuse= needs a better name (|_reuse=)
if frame.args._reuse and ('' ~= frame.args._reuse) then -- if there is |_reuse= and it's not empty
reuse_list = mw.text.split (frame.args._reuse, "%s*,%s*"); -- make a table from its contents
end
if frame.args['_alias-map'] and ('' ~= frame.args['_alias-map']) then -- if there is |_alias-map= and it's not empty
alias_map = alias_map_get (frame.args['_alias-map']); -- make a table from its contents
end
template = 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
return nil; -- not present, tell calling function to emit an error message
end
_include_positional = 'yes' == frame.args['_include-positional']; -- when true pass all positional parameters along with non-excluded named parameters to ...
-- ... the working template; positional parameters are not excludable
local _pframe_args = frame:getParent().args; -- here we get the wrapper template's 'live' parameters from pframe.args
local pframe_args = {}; -- a local table that we can modify
for k, v in pairs (_pframe_args) do -- make a copy that we can modify
pframe_args[k] = v;
end
-- here we look for pframe parameters that are aliases of canonical parameter names; when found
-- we replace the alias with the canonical. We do this here because the reuse_list works on
-- canonical parameter names so first we convert alias parameter names to canonical names and then
-- we remove those canonical names from the pframe table that are reused (provided to the working
-- template through the frame args table)
for k, v in pairs (alias_map) do -- k is alias name, v is canonical name
if pframe_args[k] then -- if pframe_args has parameter with alias name
pframe_args[v] = _pframe_args[k]; -- create new canonical name with alias' value
pframe_args[k] = nil; -- unset the alias
end
end
for k, v in pairs (pframe_args) do -- do enumerated parameter alias -> canonical translation
if 'string' == type (k) then -- only named parameters can be enumerated
if alias_map[k..'#'] then -- non-enumerated alias matches enumerated parameter pattern? enumerator at end only
pframe_args[alias_map[k..'#']:gsub('#', '')] = v; -- remove '#' and copy parameter to pframe_args table
pframe_args[k] = nil; -- unset the alias
elseif k:match ('%d+') then -- if this parameter name contains digits
local temp = k:gsub ('%d+', '#'); -- make a copy; digits replaced with single '#'
local enum = k:match ('%d+'); -- get the enumerator
if alias_map[temp] then -- if this parameter is a recognized enumerated alias
pframe_args[alias_map[temp]:gsub('#', enum)] = v; -- use canonical name and replace '#' with enumerator and add to pframe_args
pframe_args[k] = nil; -- unset the alias
end
end
end
end
-- pframe parameters that are _reused are 'reused' have the form something like this:
-- |chapter=[[wikisource:{{{chapter}}}|{{{chapter}}}]]
-- where a parameter in the wrapping template is modified and then passed to the working template
-- using the same parameter name (in this example |chapter=)
-- remove parameters that will be reused
for k, v in ipairs (reuse_list) do -- k is numerical index, v is canonical parameter name to ignore
if pframe_args[v] then -- if pframe_args has parameter that should be ignored
pframe_args[v] = nil; -- unset the ignored parameter
end
end
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
end
--[[--------------------------< W R A P >----------------------------------------------------------------------
Template entry point. Call this function to 'execute' the working template
]]
local function wrap (frame)
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
template = _main (frame, args, false); -- get default and live parameters and the name of the working template
if not template then -- template name is required
return error_msg; -- emit error message and abandon if template name not present
end
return frame:expandTemplate {title=template, args=args}; -- render the working template
end
--[[--------------------------< L I S T >----------------------------------------------------------------------
Template entry point. Call this function to 'display' the source for the working template. This function added
as a result of a TfD here: Wikipedia:Templates_for_discussion/Log/2018_April_28#Module:PassArguments
This function replaces a similarly named function which was used in {{cite compare}} and {{cite compare2}}
Values in the args table are numerically indexed strings in the form 'name=value'
]]
local function list(frame, do_link)
local args = {}; -- table of default and live parameters and their values to be passed to the listed template
local template; -- the name of the listed template
template = _main (frame, args, true); -- get default and live parameters and the name of the listed template
if not template then -- template name is required
return error_msg; -- emit error message and abandon if template name not present
end
if do_link then
template = ('[[%s|%s]]'):format(frame:expandTemplate{ title='Transclude', args = {template} }, template)
end
table.sort(args)
for i = 1, #args do
local stripped = args[i]:match('^' .. i .. '=([^=]*)$')
if stripped then args[i] = stripped else break end
end
return frame:preprocess(table.concat({
'<code style="color:inherit; background:inherit; border:none;">{{',
template,
('<wbr><nowiki>|%s</nowiki>'):rep(#args):format(unpack(args)), '}}</code>'})); -- render the template
end
local function link (frame)
return list(frame, true)
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return {
link = link,
list = list,
wrap = wrap,
};