跳转到内容

模組:TemplateArgPassingTool

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

函數說明

passArgs

呼叫模板,並將外層模板收到的參數,全數傳遞進內層模板。

使用方法:{{#invoke:TemplateArgPassingTool|passArgs|template_name=<模板名稱>}}
例如在模板內輸入{{#invoke:TemplateArgPassingTool|passArgs|template_name=P2}}
調用模板{{<模板名稱>| 第一參數 | 第二參數 }}→「第二參數」
對比直接調用{{P2 | 第一參數 | 第二參數 }}→「 第二參數 」

passEscapeArgs

呼叫模板,並將外層模板收到的參數全數進行html字元跳脫處理,並傳遞進內層模板。適合用於呼叫可能會有放入網頁標籤內之內容的模板,來避免網頁標籤損壞。

使用方法:{{#invoke:TemplateArgPassingTool|passEscapeArgs|template_name=<模板名稱>}}
例如,直接調用{{Anchor}}內容輸入2i的話:
{{Anchor|{{math|2''i''}}}}→「<span class="anchor" id="2i">」(網頁span標籤損壞)
輸出內容為<span class="anchor" id="<span class="serif"><span class="texhtml" >2''i''</span></span>"></span>
如果使用passEscapeArgs函數:
{{#invoke:TemplateArgPassingTool|passEscapeArgs|template_name=Anchor|{{math|2''i''}}}}→「」(網頁span標籤正常)
輸出內容為<span class="anchor" id="'"`UNIQ--templatestyles-00000011-QINU`"'<span class="serif"><span class="texhtml" >2''i''</span></span>"></span>

passNArgs

呼叫模板,但跳過N個參數,例如跳過1個參數表示模板外層參數的第1參數被跳過、第2參數變成第1參數、第3參數變成第2參數,以此類推。

使用方法:{{#invoke:TemplateArgPassingTool|passNArgs|skip=<跳過的參數數量>|template_name=<模板名稱>}}
例如在模板內輸入{{#invoke:TemplateArgPassingTool|passNArgs|skip=1|template_name=P2}}
調用模板{{<模板名稱>| 第一參數 | 第二參數 | 第三參數 }}→「第三參數」
對比直接調用{{P2 | 第一參數 | 第二參數 }}→「 第二參數 」

local p={}
local lib_arg={}

local module_pattern = "#%s*[Ii][Nn][Vv][Oo][Kk][Ee]%s*:%s*(.+)%s*%|%s*(.+)"

function p.passArgs(frame)
	return p.passNArgs(frame, true)
end

function p.passEscapeArgs(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local template_name = '' .. (args.template_name or 'void')
    local module_name, function_name = nil,nil
    if mw.ustring.find(template_name, module_pattern) then
		mw.ustring.gsub(template_name, module_pattern, function(str, fun)module_name, function_name = str, fun return str end)
    end
    local escape_args = {}
    for key, value in pairs(args) do
    	local arg_value = value
    	if mw.isSubsting() then
			arg_value = mw.ustring.gsub(value, "[=%|%{%}]", function(esc_str)
				local mapping = {} mapping['|']='!' mapping['=']='=' mapping['{']='(' mapping['}']=')'
				if mapping[esc_str] then
					return "{{" .. mapping[esc_str] .. "}}"
				end
				return esc_str
			end)
		end
    	escape_args[key] = mw.text.encode(arg_value)
    end
    local body_frame = working_frame:newChild{ title = module_name or template_name, args = escape_args }
	if module_name ~= nil then
		module_body = require("Module:"..module_name)
		if module_body ~= nil then
			func_body = module_body[function_name]
			if func_body ~= nil then
				return func_body(body_frame)
			end
		end
		return ''
	end
	return body_frame:expandTemplate{ title = template_name, args = escape_args }
end

function p.passNArgs(frame, no_skip)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local skip_args = no_skip and 0 or (tonumber(args.skip) or 1)
    local template_name = '' .. (args.template_name or 'void')
    local module_name, function_name = nil,nil
    if mw.ustring.find(template_name, module_pattern) then
		mw.ustring.gsub(template_name, module_pattern, function(str, fun)module_name, function_name = str, fun return str end)
    end
    local sikp_args = {}
    for key, value in pairs(args) do
    	local i = tonumber(key)
    	local new_key = key
    	local skip_flag = false
    	if i ~= nil then
    		if i > 0 then
	    		if i <= skip_args then skip_flag = true end
	    		new_key = i - skip_args
    		end
    	end
    	if not skip_flag then sikp_args[new_key] = value end
    end
    local body_frame = working_frame:newChild{ title = module_name or template_name, args = sikp_args }
	if module_name ~= nil then
		module_body = require("Module:"..module_name)
		if module_body ~= nil then
			func_body = module_body[function_name]
			if func_body ~= nil then
				return func_body(body_frame)
			end
		end
		return ''
	end
	return body_frame:expandTemplate{ title = template_name, args = sikp_args }
end

return p