Module:DemoTemplate

 Documentation[voir] [modifier] [historique] [purger]

Utilisation modifier

Ajouter #invoke:DemoTemplate| au début d'un modèle pour le transfomer en démo de ce modèle. Par exemple

{{#invoke:DemoTemplate|Date|25|décembre|1916|julien=oui|âge=oui}} 

affiche : {{Date|25|décembre|1916|âge=oui|julien=oui}}25 décembre 1916 ( dans le calendrier grégorien) (107 ans)

Ce module gère correctement les paramètres « non nommé » avec signe égal. Ainsi

{{#invoke:DemoTemplate|Rouge|1=Paramètre avec contenant un signe = (égal)}}

affiche : {{Rouge|1=Paramètre avec contenant un signe = (égal)}}Paramètre avec contenant un signe = (égal)

Par contre il ne gère pas les pipe, parenthèses, crochet... donc

{{#invoke:DemoTemplate|Rouge|foo{{!}}bar}}

affiche : {{Rouge|foo|bar}}foo

local mt = {}

function mt.__index( t, title )
	return function( frame )
		local wikiTable = {}
		table.insert( wikiTable, '{{' .. title )
		local ipairsArgs = {}
		for k, v in ipairs( frame.args ) do
			if string.find( v, '=', 1, true) then
				break
			end
			ipairsArgs[k] = true
			table.insert( wikiTable, '|' .. v )
		end
		for k, v in pairs( frame.args ) do
			if not ipairsArgs[k] then
				table.insert( wikiTable, '|' .. k .. '=' .. v )
			end
		end
		table.insert( wikiTable, '}}' )
		local wikiText = table.concat( wikiTable )
		-- rather than calling expandTemplate with the title and args we have, call preprocess, so that our code example will always match our output, even in the cases of pipes or other things we should have escaped but didn't
		return string.format(
			'<code>%s</code> &rarr; %s', 
			mw.text.nowiki( wikiText ), 
			frame:preprocess( wikiText )
		)
	end
end

return setmetatable({}, mt)