文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

此模組是為了預防不應替換引用的模板利用替換引用導入參數(自我替換引用模板)。

用法

编辑
{{{{{|safesubst:}}}#invoke:Unsubst|$N=[模板名稱]|$B=

 [ ... 模板的其餘內容 ... ]

}}

參數「$B」為不替換引用時的模板內容。除了$B$N外所有傳入的參數皆視為是替換引用後的模板代碼,參數__DATE__會自動轉換成時間(Y年n月)。

有些模板有加上<noinclude>但未在結尾加上</noinclude>,此時必須補上。

範例

编辑

正常時模板應使用此格式:

{{ {{{|safesubst:}}}#invoke:Unsubst||$N=Example|參數1=值1|date=__DATE__ |$B=

 [ ... 模板的其餘內容 ... ]

}}<noinclude>
{{doc}}
</noinclude>
原始碼 結果
Template:Tlc Template:Tlc
Template:Tlc Template:Tlc
Template:Tlc Template:Tlc
Template:Tlc Template:Tlc

local p = {}

local specialParams = {
	['$N'] = 'template name', -- Deprecated, but keeping until it is removed from transcluding templates
	['$B'] = 'template content',
}

p[''] = function ( frame )
	if not frame:getParent() then
		error( '{{#invoke:Unsubst|}} makes no sense without a parent frame' )
	end
	if not frame.args['$B'] then
		error( '{{#invoke:Unsubst|}} requires parameter $B (template content)' )
	end
	
	if mw.isSubsting() then
		---- substing
		-- Combine passed args with passed defaults
		local args = {}
		for k, v in pairs( frame.args ) do
			if not specialParams[k] then
				if v == '__DATE__' then
					v = mw.getContentLanguage():formatDate( 'Y年n月' )
				end
				args[k] = v
			end
		end
		for k, v in pairs( frame:getParent().args ) do
			args[k] = v
		end

		-- Build an equivalent template invocation
		-- First, find the title to use
		local titleobj = mw.title.new(frame:getParent():getTitle())
		local title
		if titleobj.namespace == 10 then -- NS_TEMPLATE
			title = titleobj.text
		elseif titleobj.namespace == 0 then -- NS_MAIN
			title = ':' .. titleobj.text
		else
			title = titleobj.prefixedText
		end

		-- Build the invocation body with numbered args first, then named
		local ret = '{{' .. title
		for k, v in ipairs( args ) do
			if string.find( v, '=', 1, true ) then
				-- likely something like 1=foo=bar, we need to do it as a named arg
				break
			end
			ret = ret .. '|' .. v
			args[k] = nil
		end
		for k, v in pairs( args ) do
			ret = ret .. '|' .. k .. '=' .. v
		end
		
		return ret .. '}}'
	else
		---- Not substing
		-- Just return the "body"
		return frame.args['$B'] .. (frame.args['$N'] and frame:getParent():getTitle() == mw.title.getCurrentTitle().prefixedText and '[[Category:使用$N的Module:Unsubst调用]]' or '')
	end
end

return p