Database changes have finished applying - please report any issues you're (still) seeing to support@shoutwiki.com.

Module:DemoTemplate

From TechInfoDepot
Jump to navigationJump to search
Documentation icon Module documentation[view] [edit] [history] [purge]

Usage

Simply place "#invoke:DemoTemplate|" before a call to a template. For example, {{#invoke:DemoTemplate|Convert|10|km|nmi|abbr=off}} yields the following:

{{Convert|10|km|nmi|abbr=off}} → 10 kilometres (5.4 nautical miles)

This module handles both named and positional parameters as well as equals signs in both parameter names and values correctly, so this (for example) will work: {{#invoke:DemoTemplate|1x|1=Some parameter with an = sign in it}}

{{1x|1=Some parameter with an = sign in it}} → Some parameter with an = sign in it

Note, however, that pipes, curly braces, etc. are not currently handled correctly by this module, so this (for example) will not work correctly: {{#invoke:DemoTemplate|1x|foo{{!}}bar}}

{{1x|foo|bar}} → foo

 1 require('Module:No globals')
 2 
 3 local newBuffer = require('Module:OutputBuffer')
 4 local mt = {}
 5 
 6 function mt.__index(t, title)
 7 	return function(frame)
 8 		local getBuffer, print, printf = newBuffer()
 9 		printf('{{%s', title)
10 		local ipairsArgs = {}
11 		for k,v in ipairs(frame.args) do
12 			if string.find(v, '=', 1, true) then
13 				break
14 			end
15 			ipairsArgs[k] = true
16 			printf('|%s', v)
17 		end
18 		for k,v in pairs(frame.args) do
19 			if not ipairsArgs[k] then
20 				printf('|%s=%s', string.gsub(k, '=', '{{=}}'), v)
21 			end
22 		end
23 		print('}}')
24 		local buffer = getBuffer()
25 		-- 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
26 		return string.format('<code>%s</code> &rarr; %s', mw.text.nowiki(buffer), frame:preprocess(buffer))
27 	end
28 end
29 
30 return setmetatable({}, mt)