Module:Template invocation
- Afrikaans
- Аԥсшәа
- العربية
- Արեւմտահայերէն
- Asturianu
- Azərbaycanca
- تۆرکجه
- Basa Bali
- বাংলা
- Беларуская
- Беларуская (тарашкевіца)
- भोजपुरी
- Bikol Central
- Bosanski
- Чӑвашла
- Corsu
- Dansk
- Ελληνικά
- Euskara
- فارسی
- Føroyskt
- Galego
- ГӀалгӀай
- ગુજરાતી
- 한국어
- Ilokano
- Bahasa Indonesia
- Íslenska
- ಕನ್ನಡ
- Kurdî
- Ladin
- Latina
- Latviešu
- Lietuvių
- Magyar
- मैथिली
- Македонски
- മലയാളം
- मराठी
- Bahasa Melayu
- 閩東語 / Mìng-dĕ̤ng-ngṳ̄
- Мокшень
- Монгол
- မြန်မာဘာသာ
- Nederlands
- नेपाली
- 日本語
- Norsk bokmål
- ଓଡ଼ିଆ
- Oʻzbekcha / ўзбекча
- ਪੰਜਾਬੀ
- Pangasinan
- Papiamentu
- Português
- Română
- Русский
- Scots
- සිංහල
- Simple English
- سنڌي
- Slovenščina
- کوردی
- Српски / srpski
- Svenska
- Tagalog
- தமிழ்
- တႆး
- ไทย
- Тоҷикӣ
- ತುಳು
- Türkçe
- Удмурт
- Українська
- Tiếng Việt
- Walon
- 粵語
- 中文
- ꠍꠤꠟꠐꠤ
- ᥖᥭᥰ ᥖᥬᥲ ᥑᥨᥒᥰ
Appearance
From Wikipedia, the free encyclopedia
This is a meta-module for producing MediaWiki template invocations.
Warning This Lua module is used on approximately 3,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
Usage
First, load the module:
localmTemplateInvocation=require('Module:Template invocation')
Then you can use the individual functions as documented below.
Name
mTemplateInvocation.name(title)
This function produces the name to be used in a template invocation. For pages in the template namespace it returns the page name with no namespace prefix, for pages in the main namespace it returns the full page name prefixed with ":", and for other pages it returns the full page name. title can be a string or a mw.title object.
Invocation
mTemplateInvocation.invocation(name,args,format)
This function creates a MediaWiki template invocation.
Parameters:
- name – the name of the template (string, required). This should be exactly as it will appear in the invocation, e.g. for Template:Example use "Example". To generate the template name from a page name, you can use the name function.
- args – the arguments to use in the invocation (table, required). Table keys and values must be either strings or numbers.
- format – the format of the invocation (string, optional). The default is a normal invocation with unescaped curly braces, pipes, and equals signs. If this parameter is the string "nowiki", then the curly braces, pipes and equals signs are replaced with the appropriate HTML entities.
Example
The code mTemplateInvocation.invocation('foo',{'bar','baz',abc='def'})
would produce {{foo|bar|baz |abc=def}}
.
The above documentation is transcluded from Module:Template invocation/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.
-- This module provides functions for making MediaWiki template invocations. localcheckType=require('libraryUtil').checkType localp={} ------------------------------------------------------------------------ -- Name: p.name -- Purpose: Find a template invocation name from a page name or a -- mw.title object. -- Description: This function detects whether a string or a mw.title -- object has been passed in, and uses that to find a -- template name as it is used in template invocations. -- Parameters: title - full page name or mw.title object for the -- template (string or mw.title object) -- Returns: String ------------------------------------------------------------------------ functionp.name(title) iftype(title)=='string'then title=mw.title.new(title) ifnottitleor#title.prefixedText==0or#title.interwiki>0then error("invalid title in parameter #1 of function 'name'",2) end elseiftype(title)~='table'ortype(title.getContent)~='function'then error("parameter #1 of function 'name' must be a string or a mw.title object",2) end iftitle.namespace==10then localtext=title.text localcheck=mw.title.new(text,10) -- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo". returncheckandmw.title.equals(title,check)andtextortitle.prefixedText elseiftitle.namespace==0then return':'..title.prefixedText else returntitle.prefixedText end end ------------------------------------------------------------------------ -- Name: p.invocation -- Purpose: Construct a MediaWiki template invocation. -- Description: This function makes a template invocation from the -- name and the arguments given. Note that it isn't -- perfect: we have no way of knowing what whitespace was -- in the original invocation, the named parameters will be -- alphabetically sorted, and any parameters with duplicate keys -- will be removed. -- Parameters: name - the template name, formatted as it will appear -- in the invocation. (string) -- args - a table of template arguments. (table) -- format - formatting options. (string, optional) -- Set to "nowiki" to escape, curly braces, pipes and -- equals signs with their HTML entities. The default -- is unescaped. -- Returns: String ------------------------------------------------------------------------ functionp.invocation(name,args,format) checkType('invocation',1,name,'string') checkType('invocation',2,args,'table') checkType('invocation',3,format,'string',true) -- Validate the args table and make a copy to work from. We need to -- make a copy of the table rather than just using the original, as -- some of the values may be erased when building the invocation. localinvArgs={} fork,vinpairs(args)do localtypek=type(k) localtypev=type(v) iftypek~='string'andtypek~='number' ortypev~='string'andtypev~='number' then error("invalid arguments table in parameter #2 of ".. "'invocation' (keys and values must be strings or numbers)",2) end invArgs[k]=v end -- Get the separators to use. localseps={ openb='{{', closeb='}}', pipe='|', equals='=' } ifformat=='nowiki'then fork,vinpairs(seps)do seps[k]=mw.text.nowiki(v) end end -- Build the invocation body with numbered args first, then named. localret={} ret[#ret+1]=seps.openb ret[#ret+1]=name fork,vinipairs(invArgs)do iftype(v)=='string'andv:find('=',1,true)then -- Likely something like 1=foo=bar which needs to be displayed as a named arg. else ret[#ret+1]=seps.pipe ret[#ret+1]=v invArgs[k]=nil-- Erase the key so that we don't add the value twice end end localkeys={}-- sort parameter list; better than arbitrary order fork,_inpairs(invArgs)do keys[#keys+1]=k end table.sort(keys, function(a,b) -- Sort with keys of type number first, then string. iftype(a)==type(b)then returna<b elseiftype(a)=='number'then returntrue end end ) localmaybeSpace=''-- First named parameter should not be separated by a space for_,vinipairs(keys)do-- Add named args based on sorted parameter list ret[#ret+1]=maybeSpace..seps.pipe ret[#ret+1]=tostring(v) ret[#ret+1]=seps.equals ret[#ret+1]=invArgs[v] maybeSpace=' ' end ret[#ret+1]=seps.closeb returntable.concat(ret) end returnp