室上性心动过速是什么原因引起的| 静推是什么意思| 当今社会做什么赚钱| 男同是什么| 什么样的人容易低血糖| 合是什么生肖| 孕初期有什么症状| 猪油蒙了心是什么意思| 酵母菌属于什么菌| 脚踝浮肿是什么原因引起的| pedro是什么牌子| 眼睛经常有眼屎是什么原因| 鼻头长痘痘什么原因| 四平八稳是什么意思| 鸡皮肤是什么原因引起的| 梦到抓了好多鱼是什么意思| 强心针是什么| 不要问为什么| 风向标是什么意思| 纳入是什么意思| 淋巴细胞减少说明什么| 为什么会全身酸痛| 肝内胆管结石是什么意思| 老年人吃什么钙片补钙好| 为什么医院不开金刚藤| 什么下什么什么| xg是什么意思| 贫血会引起什么症状| 什么东西吃蟑螂| 双肺纹理增多什么意思| 匹诺曹什么意思| 大三阳是什么| 打眼是什么意思| e6是什么意思| 感冒吃什么恢复快| 肺炎吃什么药| 急性咽喉炎吃什么药好得快| 4月12号是什么星座| 吃什么可以解酒| 缘木求鱼是什么意思| 补肾壮阳吃什么效果好| 蚊子怕什么味道| 醋栗是什么东西| 从此萧郎是路人是什么意思| ram是什么动物| 公务员是什么职业| 如果你是什么那快乐就是什么| 水淀粉是什么东西| 血压低有什么危险| 一喝酒就脸红是什么原因| 脚抽筋什么原因| 减肥期间可以吃什么| 时光荏苒是什么意思| 什么叫肾功能不全| 做梦杀人了是什么征兆| 陈慧琳属什么生肖| 前列腺增生吃什么药见效快| 感冒咳嗽挂什么科| 什么心丧气| 白细胞减少吃什么药| 中山市有什么大学| 农历六月十九是什么日子| 克拉是什么单位| 3911农药主要治什么| 卤水是什么东西| 2027年属什么生肖| 割包皮是什么意思| 万宝龙手表什么档次| 眼睛有重影是什么原因| 孩子一直咳嗽不好是什么原因| 水泡型脚气用什么药好| 蜗牛为什么怕盐| 菊花是什么颜色| 叒怎么读音是什么意思| 什么是滑精| 观音菩萨的坐骑是什么| 粘胶是什么材质| aah是什么意思| 腐女是什么意思| 头疼呕吐是什么原因| 什么水果降火效果最好| 充军是什么意思| momax是什么牌子| 胸为什么会下垂| 幽门螺旋杆菌阳性代表什么| 葡萄套袋前打什么药| 来月经前有褐色分泌物是什么原因| 夏天是什么时候| 油嘴滑舌指什么生肖| 检查胰腺做什么检查| 什么饭不能吃| 山楂搭配什么泡水喝好| 舍本逐末是什么意思| 纠葛是什么意思| 喝酒后呕吐是什么原因| 背疼是什么原因| 凤凰长什么样| 尿白细胞阳性什么意思| domyos是什么牌子| 白细胞计数偏低是什么原因| 植物纤维是什么面料| 94年属什么的| 吃什么利尿最快去腹水的| 工匠精神的核心是什么| 固执己见是什么意思| 左侧附件区囊性占位是什么意思| 牙龈出血是什么病的前兆| 四川九寨沟什么时候去最好| 线雕是什么| 不长头发是什么原因| 高血压吃什么药最好| 一什么桥| 蜗牛为什么怕盐| 部队指导员是什么级别| 吃什么可以补黄体酮| 刚愎自用是什么意思| 甲状腺炎吃什么药好得快| 什么原因会导致尿路感染| 妈妈的手像什么| 肚脐眼周围痛挂什么科| 什么是制片人| 递增是什么意思| 凤尾是什么菜| 女生做彩超是检查什么| 嘴唇有点发黑是什么原因引起的| 欧皇是什么意思| 肚子胀气吃什么食物| 龟代表什么生肖| mct是什么| 收口是什么意思| 尿培养是检查什么病| 末次月经是什么意思| 慈禧和光绪是什么关系| 胃酸胃胀吃什么药| 三点水一个高念什么| 杵状指常见于什么病| 天宫是什么意思| 胎动突然减少是什么原因| 海是什么颜色| 寒疾现代叫什么病| 肚子痛挂什么科| 1800年是什么朝代| 吃薄荷对人身体有什么好处| 3月是什么季节| 绯是什么意思| 宇宙的尽头是什么| msv是什么单位| 维生素d缺乏吃什么药| 指甲凹凸不平什么原因| 褶是什么意思| 蜜蜡五行属什么| 五大仙家什么仙最厉害| 左下腹是什么部位| 什么人容易得梦游症| 什么的杨桃| 戴玉有什么好处| haccp认证是什么意思| 气虚用什么泡水喝好| 9月16号是什么星座| 5月份出生的是什么星座| 扑尔敏的学名叫什么| 什么可以代替人体润滑油| 紫苏有什么功效与作用| 花洒不出水什么原因| 阑尾炎吃什么药最有效| 警察两杠三星是什么级别| 小便失禁是什么原因男性| 财神爷供奉什么供品| 狗拉稀吃什么药| 梦见借给别人钱是什么意思| 磨牙是缺什么| 失足妇女是什么意思| 梦到大牙掉了一颗是什么意思| 身体有异味是什么原因| 血象高会导致什么后果| 4090是什么意思| jet是什么意思| 婴儿外阴粘连挂什么科| 大便不规律是什么原因| 许嵩的粉丝叫什么| 路演是什么意思| hip是什么意思| 赵本山什么学历| 01年属蛇的是什么命| 没有美瞳护理液用什么代替| 梯是什么意思| 看头发应该挂什么科| 糖化血红蛋白是什么| 什么家欢乐| 甲钴胺片治疗什么病| 心肾两虚吃什么中成药| 休克是什么症状| 北京大学校长什么级别| 查过敏源挂什么科| 产后抑郁一般发生在产后什么时间| 经常梳头有什么好处| 冠心病吃什么药最好| 拔罐起水泡是什么原因| 月结是什么意思| 落成是什么意思| 老母鸡炖什么好吃又有营养价值| 学信网上的报告编号是什么| 清明节什么时候| 心火旺失眠吃什么药| 北芪煲汤加什么药材好| 娘娘命是什么样的命| 雨伞代表什么数字| 蝙蝠是什么动物| 阴道里面痒是什么原因| 机油用什么能洗掉| 马桶对着卫生间门有什么不好| 桑葚和枸杞泡水喝有什么好处| 老年人打嗝不止是什么原因| 生日送什么| 二五八万是什么意思| 小孩子肚子疼吃什么药| 头菜是什么菜| 怀孕前有什么征兆| 什么大什么功| 内分泌失调吃什么药效果最好| 谐星是什么意思| 脊柱侧弯是什么原因引起的| 甲钴胺是什么| 1927年属什么| 6月有什么水果| 彩礼是什么意思| 胃寒吃什么药| 什么心什么胆| 脾虚湿气重喝什么茶| 宝宝为什么喜欢趴着睡| 为什么会长黑痣| 双肺纹理增多增粗是什么意思| 百雀羚适合什么年龄段| 胎盘下缘覆盖宫颈内口是什么意思| 镜检白细胞是什么意思| 什么是大运| 五劳七伤指的是什么| 胃痛吃什么药效果最好| 二建什么时候考试| 太行山在什么地方| 吃什么能治白头发| 知了吃什么| ce是什么元素| 脚后跟长痣有什么寓意| 下属是什么意思| 感冒吃什么药最快| 孕妇喝什么水比较好| 瘘管是什么| 什么是金融行业| 眼睛红了是什么原因| 结局he是什么意思| 电脑什么牌子好| 天蝎座有什么特点| 尿酸高什么原因引起的| 正方体体积公式是什么| 同字五行属什么| 吃火龙果有什么好处和坏处| 天庭饱满是什么意思| 绒毛浆是什么| 什么是管制| 什么可以美白牙齿| 非食健字是什么意思| 耳朵真菌感染用什么药| 氮肥是什么肥| 百度Jump to content

您的好友黄梦莹已上线 “素锦”演绎摩登仙女杀

Page semi-protected
From Wikipedia, the free encyclopedia
(Redirected from Wikipedia:Template syntax)
百度 连接屏幕的转轴略有凸起,可能会硌手,但并不影响使用。

A template is a Wikipedia page created to be included in other pages. Templates usually contain repetitive material that might need to show up on a larger number of articles or pages. They are commonly used for boilerplate messages, standardized warnings or notices, infoboxes, navigational boxes, and similar purposes. Templates can have parameters so that the same page generates different text on multiple pages that include it.

The most common method of inclusion is called transclusion, where the wiki source of the target page contains a reference to the template, using the {{Template name}} syntax. Another method is substitution, where the content of the template is copied into the wiki source of the target page, just once, when it is saved.

Help:A quick guide to templates gives a brief introduction to the subject. There is further help from MediaWiki at mw:Help:Templates and mw:Manual:Advanced templates.

General description

A basic overview of how templates work (8-minute video)

Most templates are in the template namespace, which means that they have titles in the form "Template:XXXX". It is possible, however, to transclude and substitute from any namespace,[a] and so some template pages are placed in other namespaces, such as the user namespace. Template pages have associated talk pages.

Templates can contain any desired wikitext, including calls to other templates. There is some programming ability: customizable values (via parameters); calculation and branchings (using parser functions); and access to wiki-specific variables (magic words), such as dates, times, and page names. They may also contain markup to define certain parts of the page to be included while other parts are not. This means that when you display the template page itself, you may see more than when you display a page that includes it (for example, it can contain documentation, categories, etc. for the template).

To call a template (cause it to be transcluded or substituted in the page), a page contains wikitext in double braces. For example, the wikitext {{under construction}} calls the {{under construction}} template. Sometimes the call includes parameters and looks like {{under construction|comment=This is a comment}}. Note that template calls are not the only thing for which double braces are used in wikitext.

Note that while templates are the usual way transclusion and substitution happen on Wikipedia, they are not the only way.

Using templates

General

Using a template is similar to calling a function in programming. When called, it returns a value. For a template, calling is known as a template call and the return value is known as the expansion of the template, which is treated as part of the calling page's source. Like functions, some templates can take parameters that affect their output.

Calling a template causes it to be either transcluded or substituted in the page that calls it (the target page).

Transcluding a template means that when MediaWiki displays the page, it treats the template as if it were directly in the page’s source. Substituting a template differs in that it inserts the content of the template permanently into the target page when it is saved and subsequent editors cannot tell that it originated from a template.

When you update a template, every page that transcludes it automatically reflects the update when the page is displayed in the future, whereas updating a template has no effect on pages that have previously been saved with the template substituted.

To transclude a template, insert {{Template name}} at the point where it should appear in the page source. To substitute it, use {{subst:Template name}}. This wikitext is called a template call.

In MediaWiki, the wiki software that Wikipedia uses, variables are distinct from templates, though both are referenced by double curly braces {{ }} and they both return a value. These references differ in that variables in MediaWiki are always uppercase, while template names follow the same conventions and have the same limitations as page names. For example, they are case-sensitive (except for the first character), underscores are parsed as spaces, and template names cannot contain the characters # < > [ ] | { } because they are reserved for wiki markup.

The number sign # is used as a fragment identifier, which identifies a fragment or section of a page. Although you can use it in a link to link to a section of a template page (e.g., Template:Portal#Example), fragment identifiers have no meaning in a template call and are ignored. For example, {{Portal#Location|Books}} is the same as {{Portal|Books}}.

The template namespace is the default, so you can leave out the namespace Template: in the template name, and it is conventional to do so. However, you must specify the namespace prefix for templates in other namespaces, such as User:. To transclude a page in mainspace, precede its page name with a colon, as {{:Page name}}.

information Note: Attempting to transclude a template that does not exist produces a red link, just like linking to any other nonexistent page. Following the link allows one to create that particular template. It is not possible to transclude templates between projects (such as different-language Wikipedias or MediaWiki) – to use a template on another language project, a copy of the template must be created in that project.

Parameters

The basic transclusion syntax for a template can be further controlled using parameters, which allow you to customize a template’s output. The syntax is {{Template name|parameter|parameter|...}}, where Template name is the template’s name, and each parameter can be either a simple value (known as an unnamed parameter) or in the form name=value (known as a named parameter). The first, second, third, etc., unnamed parameters are named 1, 2, 3, etc., so using {{Template name|1=value1|2=value2}} is equivalent to {{Template name|value1|value2}}.

Each template has specific parameters it can accept, as defined within its code. You can list named parameters in any order in a template call. Extra or misnamed parameters have no effect, while missing parameters cause a default value to be used. If a parameter is repeated, the last defined parameter value is used.

The value of a parameter can be an empty string, such as when the pipe (|) or equals sign is followed immediately by the next pipe or the closing braces. For example, {{Template name|1=|2=test}} produces the same output as {{Template name||2=test}}. This is different from not specifying the parameter at all, which results in a default value, although templates are often coded so as to behave the same in both cases.

If a template call specifies a parameter which is not defined in the template, it has no effect. Editors sometime specify a parameter they know is not defined in the template — for example, editors sometimes include a parameter like <reason> to add a brief explanation within the source as a hidden comment. (But some templates, such as Requested move, are programmed to show the reason parameter if provided). Certain templates, especially complex ones like infoboxes, may use the check for unknown parameters module to alert editors about any unrecognized parameters they code by mistake.

Whitespace handling

Leading and trailing whitespace (including line breaks) around named parameters and values is collapsed automatically, but spaces in the middle of the value are kept. For instance, {{ ... | myparam = this is a test }} has the same output as {{ ... |myparam=this is a test}}. However, with unnamed parameters, all whitespace is retained as preserved in the output.

The collapsing of line breaks around parameters can be used to improve the readability of a template call with many parameters by placing each parameter specification in its own line.

Variable length parameter lists

In MediaWiki, templates cannot automatically handle an unknown or unlimited number of parameters. Each possible parameter usually has to be predefined in the template code. For example, a template might be set up to use three specific parameters, such as 1, 2, and 3. If someone includes additional parameters beyond those, they will not affect expansion of the template.

However, there are some ways to work around this:

  • Setting a limit: You can write the template to handle a fixed number of parameters by manually specifying each one (e.g., up to 10 or 20 parameters).
  • Using templates or modules: For cases needing a flexible number of inputs, templates can use Lua modules or helper templates (like {{#invoke:separated entries|main}} or {{separated entries}}). These helper tools provide more advanced handling, such as counting or iterating through parameters, enabling the use of multiple inputs without setting an exact number.

However, modules can bypass this limitation. For simpler cases, the separated entries module expands all sequential parameters and lets you set custom delimiters. For more advanced usage, the {{#invoke:params}} module enables counting, listing, mapping, filtering, and handling a variable number of parameters without prior knowledge of the exact number.

Templates that accepts an open number of parameters are often collected under Category:Variadic templates.

Examples

information Note: If you wish to experiment with any of these, you can use the template sandbox or your user page or sandbox. You can try out your template with Special:ExpandTemplates.

An example of a very simple template can be found at Template:TM, which expands to wikitext that places the trademark symbol (?), linked to the trademark article, at that point in the displayed page. A programmer would say that the template "returns" the trademark symbol link wikitext.

Display Template:TM (by clicking on the foregoing link), then click on the "Edit" tab to see the template code (its page source). The active part of that code, which becomes the expansion of the template when it is transcluded, is the single link wikitext [[Trademark|?]]. The remainder of the page source is enclosed between <noinclude> tags, so it shows up when you display the template page itself but not when you display a page that calls the template.

To transclude Template:TM onto another page (i.e., to use it on another page), type {{TM}} into the source of that page (the target page) and click Show preview. The page will be displayed with the template call replaced by the expansion of the template, as if the wikitext actually contained [[Trademark|?]] at that point. The displayed page will therefore contain the wikilink "?".

For example, type The trademark symbol is {{TM}} and you will see "The trademark symbol is ?" when previewing the page or after saving the change.

The other way to use a template is to substitute it. If you type The trademark symbol is {{subst:TM}} and preview or save the page, you will see "The trademark symbol is ?" just as with the transclusion example above. But if you save the page and then look again at the saved page source,[b] you will see The trademark symbol is [[Trademark|?]], because the template call was replaced by the expansion of the template when you saved the page. There is no ongoing connection to the template call; no one can tell by looking at the page source that you didn't just type The trademark symbol is [[Trademark|?]] and if the {{TM}} template changes (or ceases to exist), your page will continue to display as it does now. (In contrast, in the transclusion example above, if the {{TM}} template subsequently changes, your page may then display differently).

Examples with parameters

An example of a template that takes parameters is the template {{about}}. Try typing {{about|how to use templates|how to use modules|Help:Lua}} in the sandbox—it will produce the following text:

The template {{about}} uses three unnamed parameters (also called positional parameters) in the example above, but you can call the same template with different numbers of parameters to give slightly different results, as explained in the template's documentation. For example, {{about||how to use modules|Help:Lua}}. Note the usage of an empty parameter—in this instance, the consecutive pipes mean that the first parameter specified is an empty string, which in this template will cause it to omit the initial "about" sentence. This produces:

A simple example of a template that has named parameters is Template:Payoff matrix, used to generate a 2-by-2 grid. For example:

Markup Renders as
{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}
Left Right
Up 5 7
Down 2 9
Example usage

See the template page for more possibilities. Notice that the template is called here without specifying all its possible parameters, so undefined parameters are given default values.

Hints and workarounds

The following points may be worth noting when using templates:

  • Templates are not the only method of transclusion, in some cases, other methods such as selective transclusion are more appropriate.
  • An unnamed parameter (positional parameter) value cannot contain an ordinary equals sign, as this would be interpreted in the parameter specification as a named parameter (with the equals sign separating the name from its value).[c]
    • To specify an unnamed parameter including an equals sign (for example in a URL with name–value pairs), replace the equals sign with the magic word {{=}}, which expands to an equals sign that will not be interpreted.
    • Another method is to explicitly specify the positional parameters. The first unnamed parameter is named "1" and so on. To call template {{done}} with a=b as the literal value for the first parameter, type either {{done|a{{=}}b}} or {{done|1=a=b}}.
  • Similarly, it is not possible to use an ordinary pipe character | in a template parameter specification, as it would be interpreted as separating one parameter specification from another.[d] This problem can similarly be solved by using the magic word {{!}} in place of the pipe, or—if the pipe is not intended to be parsed at a higher level—using the HTML entity &#124;. Alternatively, for embedding wikitables in templates, you may use {{Wikitable}} to avoid excessive {{!}}.
  • Remember that whitespace characters (spaces, tabs, carriage returns, and line feeds) are not automatically stripped from the start and end of unnamed parameters, unlike with named parameters. Including such characters (or any other non-visible characters in any parameters) may in some cases affect the template's behavior in unexpected ways. (Template designers can use {{Trim}} to remove unwanted whitespace in unnamed parameters).
  • In documentation and discussions, it is customary to put the name of a template in double braces to emphasize the reference to a template (for example, use {{Trim}} as the name of Template:Trim). If you just type {{Trim}} in the source page, that will of course call the template, so to make it easy to display the name with the braces, and also make the name a link to the template for the reader's convenience, there is the {{tl}} template (the "template link" template). For example, {{tl|Example}} produces {{Example}}. There are various other template-linking templates available with other functions.
  • When an editor changes a template (by editing the template or one of its subtemplates), the change will ultimately be reflected any time any page that transcludes that template is subsequently displayed. However, the change may not become visible on all pages immediately; a previously cached version of a page, based on the previous version of the template, may continue to be displayed for some time. Use the purge function to force a page to be displayed using the latest versions of templates—including on the template page itself, if it contains usage examples.
  • When viewing old versions of pages, remember that templates will be transcluded as they are now, not necessarily as they were when the old page version was active.
  • To list all pages that transclude a template, use the What links here link on the page when you display the template page. Note that this will not include pages where the template has been substituted, as there is no record of substitutions.
  • To get a list of templates transcluded on a page, bring up the edit page and find the list below the edit window. This list also includes the subtemplates used by the templates that are directly transcluded. To get such a list for a page section, an old version of the page,[e] or your newly edited version prior to saving, click Show preview on the appropriate edit page.
  • There are limits to the number and complexity of the templates that an article may have. See the "§ Expand limits" section for help with this.
  • If you want the template to leave a time stamp or signature, you can write <noinclude><nowiki></noinclude>~~~~~<noinclude></nowiki></noinclude>, but this works only when you substitute the template. If you transclude it, you'll just get ~~~~~.
  • To improve readability, programmers usually like to split code with newlines and indent it. Unfortunately, MediaWiki does not allow this technique; in many instances, these purpose-built newlines are treated by MediaWiki as content. One possible workaround is to add <!-- before each newline character and --> after it, which produces an HTML comment.

Writing templates

Process

Templates are just Wikipedia pages. They are created, deleted, and edited in much the same way as any other page.

Creating

To create a template, choose an appropriate name, navigate to that page, then click the "Edit" tab or create a new page as needed.

Templates are normally placed in the template namespace, though you can place a template intended for your own personal use or for experimentation in your own user space.

Before creating a template, do a quick search for existing templates (such as by exploring Category:Wikipedia templates) to see if there is already a template that does what you want or a similar template whose code can be copied and modified (or left in place and expanded). Look for generic templates on which the new template can be based; for example, you can create a navbox template easily by creating a brief template that calls the generic Template:Navbox.

There is no hard rule about what name to choose for a template—make it short but reasonably descriptive. The naming guideline says: "Template function should be clear from the template name". If similar templates exist, try to follow a consistent naming pattern. You can rename a template without breaking existing transclusions (what is called breakage) by leaving a redirect to the new template name.

Modifying

Edit a template the same way as any other page: navigate to the template and click the "Edit" tab.

Be extremely careful when editing existing templates—changes made can affect a large number of pages, often in ways you might not expect. For this reason many high-use templates are protected against editing except by administrators and template editors; other editors can propose changes on the talk page. Some templates offer a sandbox and test cases for experimentation.

Deleting

Unused or inappropriate templates should be deleted. Templates that can be easily merged into another should be merged.

To propose the deletion or merger of a template, go to Templates for discussion (TfD).

Coding a template

Anything that can be included on a normal page or article can be included on a template, including other templates (called subtemplates). Templates often make use of programming features—parameters, parser functions, and other magic words—which allow the transcluded content to vary depending on context. There are also special tags to control which information is transcluded and which is not.

Metatemplates

Various metatemplates and metamodules exist to help accomplish common template tasks. They are called like normal templates and modules, but they serve a purpose that makes writing templates easier. See Category:Wikipedia metatemplates and Category:Template metamodules for a list of those templates and modules.

Handling parameters

In template code, the value of a parameter is represented by items enclosed between triple braces, which is a parameter reference.

  • The code {{{xxx}}} expands to the value of the parameter named "xxx".
  • The codes {{{1}}}, {{{2}}}, and so on are expanded to the first, second, and so on unnamed parameters. (Note that an unnamed parameter can alternatively be specified in a template call as an equivalent named parameter named "1", "2", etc.).

If a parameter is not specified in the template call, then the parameter reference is not replaced with anything -- it is expanded literally; this means that if the template call does not specify the parameter "xxx", the wikitext {{{xxx}}} inside the template expands to literally {{{xxx}}} (not the null string you may have expected). You can get a more useful behavior by specifying a default value in the parameter reference. Do this with the pipe syntax: {{{xxx|dflt}}} specifies the default value dflt for the named parameter "xxx", and {{{1|dflt}}} specifies the default value dflt for the first unnamed parameter. Most often, one specifies a null default value, such as {{{1|}}} or {{{xxx|}}}.

If a call sets a parameter to the empty string like {{Template name|xxx=}} then {{{xxx|OK}}} will produce the empty string and not the default value "OK". Many users will expect that an empty parameter gives the same result as omitting the parameter. You can achieve this with: {{#if:{{{xxx|}}}|{{{xxx}}}|OK}}. This says: If xxx is assigned a non-empty value then use xxx, otherwise use "OK". If the default value is the empty string then you only have to write {{{xxx|}}}.

You can use default parameter values to effect a parameter alias: For example, if parameters "text" and "message" are names for the same parameter, which can also be specified as the only unnamed parameter, then refer to the parameter with {{{message|{{{text|{{{1|}}}}}}}}}. If the template call specifies more than one of those parameters, "message" will have priority, followed by "text", and finally by the first unnamed parameter. So if a template call specifies parameters |message=A|text=B|C, the above wikitext expands to A.

Because of the multiple meanings of double-brace and triple-brace syntax in wikitext, expressions can sometimes be ambiguous. It may be helpful or necessary to include spaces to resolve such ambiguity. For example, {{ {{{xxx}}} }} or {{{ {{xxx}} }}}, rather than typing five consecutive braces, may be more human-readable. But watch out for unwanted whitespace appearing in the template expansion.

Parameter references do not get expanded when they are wrapped in <nowiki> tags.

Example

The parameter usage example above refers to the {{payoff matrix}} template. Here is the code in the template that implements those parameters:

{| id="Payoff matrix" style="background:white; float: {{{Float|right}}}; clear:right; text-align:center;" align={{{Float|right}}} cellspacing=0 cellpadding=8 width={{{Width|225}}}
|-
|style="width:33%;                                                     "| 
|style="width:33%;                      border-bottom: solid black 1px;"| {{{2L|Left}}}
|style="width:33%;                      border-bottom: solid black 1px;"| {{{2R|Right}}}
|-
|style="border-right:  solid black 1px; text-align: right;             "| {{{1U|Up}}}
|style="border-right:  solid black 1px; border-bottom: solid black 1px; background:{{{ULc|white}}}; font-size:120%; "| {{{UL|0, 0}}}
|style="border-right:  solid black 1px; border-bottom: solid black 1px; background:{{{URc|white}}}; font-size:120%; "| {{{UR|0, 0}}}
|-
|style="border-right:  solid black 1px; text-align: right;             "| {{{1D|Down}}}
|style="border-right:  solid black 1px; border-bottom: solid black 1px; background:{{{DLc|white}}}; font-size:120%; "| {{{DL|0, 0}}}
|style="border-right:  solid black 1px; border-bottom: solid black 1px; background:{{{DRc|white}}}; font-size:120%; "| {{{DR|0, 0}}}
|-
|style="font-size: 90%;" colspan=3 |''{{{Name|{{PAGENAME}}}}}''
|}

The entity {{{2L|Left}}} instructs the template to use the value of the named parameter 2L or the text Left if the parameter is not specified in the call.

Special case: parameters within an XML-style opening tag

Parameter references aren't expanded inside XML-style opening tags. Thus, the following will not work within a template:

  • <ref name={{{param}}}> Smith, Adam (1776)...</ref>

because the parameter is not expanded. Instead, you can use the {{#tag:}} parser function, which is—for example—used in {{sfn}} to generate the <ref>...</ref> element; see also Help:Magic words § Formatting. Therefore, the following example will work:

  • {{#tag:ref | Smith, Adam (1776)... | name={{{param}}} }}

Caution: overextending URLs

If a parameter's value is (or ends with) a URL, check whether it is displayed in Wikipedia with the link overextending by one or more characters after the URL so that clicking the link causes an error or failure. This could happen because the source code does not have a space after the URL or it contains or generates a space that is discarded in the processing. Ensure that in the template expansion a soft space (not a hard or non-breaking space) follows the URL, regardless of whether you or a user supplied the URL or whether it was generated by automated processing. The {{spaces}} template may be useful.

System variables and conditional logic

Template code often makes use of the variables and parser functions described at Help:Magic words to make the template's behavior depend on the environment in which it is included (such as the current time or namespace). Parser functions can be used for some arithmetic calculations and string manipulations on variables and parameter values, but certain standard programming features such as loops and variable assignment are not available. Full string manipulation is not available; some templates providing such function have been created, but they are inefficient and imperfect.

Some of the most frequently used variables and functions are listed below. For more, see Help:Magic words and the fuller documentation at the MediaWiki pages mw:Help:Magic words and mw:Help:Extension:ParserFunctions.

Examples of core parser functions
Description wiki source Displayed text
Uppercasing text {{uc: Heavens to BETSY! }} HEAVENS TO BETSY!
Lowercasing text {{lc: Heavens to BETSY! }} heavens to betsy!
Getting a namespace name {{NS: 1 }} Talk
Getting a Wikipedia URL {{fullurl: pagename }} //en.wikipedia.org/wiki/Pagename

The ParserFunctions extension provides more programming-oriented parser functions:

Examples of extension parser functions
Description Wiki source Displayed text
Testing for equality between two strings (or parameters). If the first two parameters are equal, the third parameter is returned, otherwise the fourth parameter is returned. {{#ifeq: yes | yes | Hooray...! | Darn...! }} Hooray...!
{{#ifeq: yes | no | Hooray...! | Darn...! }} Darn...!
Testing whether a string (or parameter) contains anything (other than whitespace). If it does, the second parameter is returned, otherwise the third parameter is returned. {{#if: {{{param|}}} | Hooray...! | Darn...! }} Darn...!
Making a calculation (mathematics)
[area of circle of radius 4, to 3 decimal places]
{{#expr: ( pi * 4 ^ 2 ) round 3 }} 50.265
Testing the result of a calculation. If the expression is true or non-zero, the second parameter is returned, otherwise the third parameter is returned.
[is 1230 even or odd?]
{{#ifexpr: 1.23E+3 mod 2 | Odd | Even }} Even
Examples of system variables
Description Wiki source Displayed text (for this help page)
Page names {{PAGENAME}} Template
{{FULLPAGENAME}} Help:Template
Name of the current namespace {{NAMESPACE}} Help
Number of registered users {{NUMBEROFUSERS}} 49,501,209
Number of pages in a given category {{PAGESINCATEGORY:"Weird Al" Yankovic albums}} 20
Current software version {{CURRENTVERSION}} 1.45.0-wmf.12 (4f0e620)
Timestamp of last page revision {{REVISIONTIMESTAMP}} 20250727042938

The {{PAGENAME}} and {{NAMESPACE}}variables are particularly useful, and frequently used, to change template behavior based on the context in which they are included. Templates that contain category links often do this. For example, a cleanup template contains a category link to categorize the calling page as one which needs cleanup, so the template is likely to condition that category link on the {{NAMESPACE}} variable so that talk pages, user pages, and any other pages that might call the template incidentally do not get categorized as pages needing cleanup.

Nesting templates

A template may call another template—this is called nesting and the called template is called, in this context, a subtemplate. When WikiMedia expands the template, it expands subtemplates as the calls to them appear, so that the final product is essentially the result of expanding templates from the most deeply nested out.

While fairly straightforward in application, it involves some noteworthy quirks and tricks.

To pass a parameter value from a template call to a subtemplate, use a parameter reference in the template call to the subtemplate.

Example:
Template:A contains "the quick brown {{B|{{{3}}} }} jumps over...". Template:B (a subtemplate) contains '''{{{1}}}'''. Page X calls A with {{A|apple|pear|fox}} This expands to "the quick brown '''fox''' jumps over...". The third unnamed parameter passed to Template:A gets passes as the first unnamed parameter to subtemplate B.

A template can even choose which subtemplate parameter to pass conditionally.

Examples:
Template:A contains the quick brown {{B|{{{3}}}=fox}} jumps over.... Template:B (a subtemplate) contains '''{{{jumper}}}'''. Page X calls A with {{A|apple|pear|jumper}}. This expands to "the quick brown '''fox''' jumps over...". The third unnamed parameter passed to Template:A is passed as the name of the parameter passed to subtemplate B with the value "fox".

Template recursion is not available; that is, a template may not call itself directly, or indirectly by calling other templates which call it. Attempts to do so will result in an error message describing a "template loop".

When a subtemplate contains unmatched braces—as in {{lb}}}—the unmatched braces are treated as text during processing—they do not affect the parsing of braces in the calling template. But where the template is substituted, the unmatched braces will be parsed as braces when the page is subsequently displayed. This has little practical use, but can occasionally introduce unexpected errors.

See mw:Manual:Advanced templates and mw:Manual:Recursive conversion of wikitext for more information.

Inclusion control: noinclude, includeonly, and onlyinclude

By default, when a page calls a template, MediaWiki includes the expansion of the entire template in the calling page. However, it is possible to modify that behavior, using tags that specify which parts of the template code are to be included. This makes it possible for the template to contain information only for display when the template page itself is displayed, such as the template's documentation, or categories. It is also possible to have parts of the template be included in calling pages, but not be displayed when the template page itself is displayed and not be processed when the template page itself is saved (e.g., categories to be applied to calling pages which do not apply to the template). The tags are as follows:

  • <noinclude>...</noinclude> – The text between the tags is not included when the template is called, but is processed when the template itself is displayed or saved; a common use is for documentation in templates.
  • <onlyinclude>...</onlyinclude> – Nothing on the page except what appears between the tags is included when the template is called.
  • <includeonly>...</includeonly> – The text between the tags is included when the template is called, but is not processed when the template is displayed or saved.
Wikitext What is rendered here (template page) What is included there (calling page)
<noinclude> text1 </noinclude> text2 text1 text2 text2
<onlyinclude> text1 </onlyinclude> text2 text1 text2 text1
<includeonly> text1 </includeonly> text2 text2 text1 text2
<onlyinclude><includeonly> text1 </includeonly></onlyinclude> text2 text2 text1

Perhaps the most common issue with the use of these blocks is unwanted spaces or lines. It is important to remember that the effect of these tags begins immediately before the first angle bracket, not on the previous line or at the previous visible character; similarly the effect ends immediately after the last angle bracket, not on the next line or with the next visible character. For example:

}}<includeonly>
}}
</includeonly>

These tags can be nested inside each other, though (for a given page) this is really meaningful only for the <onlyinclude> tag; nesting <includeonly> and <noinclude> tags is fairly pointless. Be careful to properly nest the tags, however. Constructions like <onlyinclude>abc<includeonly>def</onlyinclude>ghi</includeonly> will not work as expected. Use the "first opened, last closed" rule that is standard for HTML/XML.

Problems and workarounds

  • The following techniques are helpful in debugging a template:
    • Use Special:ExpandTemplates to see the full recursive expansion of one or more templates.
    • Use subst: to substitute a template (rather than transclude it), which can show more clearly what is happening when the template is transcluded; see Help:Substitution.
    • Use msgnw: (short for "message, nowiki") to more-or-less transclude the source of the template rather than its expansion. It is not perfect: lists are rendered, comments are removed, and single newlines are replaced with spaces (which is particularly confounding when transcluding wikitext tables).
  • If the first character of a template expansion (or parser function result) is one of four wiki markup characters—:, ;, *, #[f], it is processed during display as though it were at the beginning of a line, even if the template call is not. This allows you to create various kinds of lists with templates where the template call may not be in the correct place for a list. To avoid this, use <nowiki /> before the markup or <nowiki>...</nowiki> around it, or use the HTML entities &#58;, &#59;, &#42;, &#35;, or use the templates {{colon}}, {{;}}, {{asterisk}}, {{number sign}} (those templates may not exist at other wikis). In some cases, the HTML entities will work when the <nowiki /> does not. The problem often occurs when a parameter value in a template call starts with one of the four characters. See also {{Encodefirst}}.
  • For issues with template substitution, such as how to control whether subtemplates are substituted as well when the parent template is substituted, see Help:Substitution.
  • You can use the template {{Trim}} to strip any initial or final whitespace from unnamed parameter values if this would cause problems; named parameter values are automatically stripped in this way.
  • To protect server resources and avoid infinite loops, the parser imposes certain limits on the depth of transclusion nesting and on the page size with expanded templates. This may cause a page to break if it uses very complex templates, particularly if there are multiple such templates on the same page. For more information, see WP:Template limits. You can check a page's overall load on the server by examining the generated HTML for a page and looking for the NewPP limit report comments.
  • Do not use = wikimarkup to create a section header in a template which is intended for use in article space; this will create an edit link on a page that transcludes the template that will confusingly open the template for editing.
  • You may avoid section edit links to the template by including <includeonly>__NOEDITSECTION__</includeonly>.

Documentation

Documentation for users, together with the template's categories, normally goes after the template code, inside <noinclude>...</noinclude> tags. It is normally necessary to put the opening <noinclude> tag immediately after the end of the code, with no intervening spaces or newlines, to avoid transcluding unwanted whitespace.

In the case of complex templates, the documentation is often kept on a separate subpage of the template page (named "Template:XXX/doc"). This applies especially to many protected templates, so that non-administrators can edit the documentation. To do this, place a call to the {{Documentation}} template after the main template code and within <noinclude>...</noinclude> tags. If the "/doc" subpage does not exist, a link appears when you display the template that you can use to create the subpage.

The documentation subpage, rather than the template itself, is normally what is placed in categories to represent a template.

Categorization

Categorize pages by template inclusion

Some templates generate category declarations in their expansion, since the template is intended to place calling pages in particular categories. This is often done with maintenance categories. Placing articles into ordinary content categories in this way is discouraged. When doing this, you may have to use <includeonly>...</includeonly> tags to keep the template itself out of the category. While developing, testing, sandboxing, or demonstrating a template intended to apply a category, either temporarily replace each category with a test category (starting with X1, X2, or X3) or suppress categorization (see category suppression in templates).

Categorize templates

Categorizing your template and documenting its proper usage will make it easier for other editors to find and use.

Category declarations for a template itself should be placed on the template's documentation subpage (or inside <noinclude>...</noinclude> tags if there is no documentation subpage) to avoid placing calling pages in the category.

Aliases

A redirect of a template functions as an alias. For example, Template:Tsh redirects to Template:Template shortcut, so you can code {{tsh|foo}} instead of {{Template shortcut|foo}}.

It is good to prepare template aliases for variations in whitespace and capitalization. For example, there is a template called {{See Wiktionary}}. The "W" is capitalized, since the word "Wiktionary" is so, but a redirect {{See wiktionary}} (with lowercase "w") exists because editors may misremember it as the latter.

Template limits

"Post-expand include size" limit. When templates are rendered or expanded to HTML for viewing in your browser, they use memory. This is called the "post-expand include size" and has a limit of 2,048,000 bytes. This size is included as an invisible comment in the HTML output—use your browser's view source feature to show the raw HTML and search for "newpp". The report will look like:

<!--
NewPP limit report
Preprocessor node count: 2382/1000000
Post-expand include size: 63476/2048000 bytes
Template argument size: 9517/2048000 bytes
Expensive parser function count: 2/500
-->

The example shows that template expansion is using 63,476 bytes out of 2,048,000 bytes of available memory.

Display problem. If too many templates are included on a page, the post-expand include size may exceed the limit. When this happens, templates after the limit will no longer expand and will instead display as a wikilink (for example, Template:Template name). Common causes are the inclusion of too many citation templates, navbox templates, and/or flag templates. To resolve this problem substitute templates, directly invoke modules, remove templates, or split the page.

Non-rendered tranclusions still count towards limit. For example, a page which contains only {{#if:{{:Main Page}}}} would still have a post-expand include size even though it would have no output at all.

The same applies to Scribunto modules. For example, {{#invoke:Test|main}} would still increase post-expand include size even if Module:Test were simply:

mw.getCurrentFrame():preprocess "{{msgnw::Main Page}}" -- remove this line and post-expand include size becomes zero
return {main = function() end} -- p.main() has no return value

Lua programming language

The Lua programming language is available for use through the Scribunto MediaWiki extension. You can embed Lua code into templates by employing the {{#invoke:}} functionality of the Scribunto MediaWiki extension. The Lua source code is stored in pages called modules, and templates invoke these individual modules. For example, you can invoke Module:Example with the code {{#invoke:Example|hello}} to print the text "Hello World!".

Terminology

A template is a Wikipedia page whose purpose is to be transcluded or substituted in another page. Templates are usually in the template namespace, but don't have to be. Pages can transclude or substitute any page, not just templates; what makes a page a template is the page's purpose.

Calling a template means transcluding or substituting one. A page calls a template. The wikitext on a page that causes the page to call a template is called a template call. For example, {{sic|constellation prize}}. Template calls are always delimited by double braces ({{}}), but not everything enclosed in double braces is a template call.

Expansion is the process of generating page source from a template, when a page calls it, by applying parameters and other features. MediaWiki expands a template as it renders a page. One can also say that the template itself expands, so both "MediaWiki expands {{{1}}} as the value of the first positional parameter" and "{{{1}}} expands as the value of the first positional parameter" work.

Expansion of a page is also the page source that results from template expansion. For example, the expansion of the {{sic}} template when called by {{sic|constellation prize}} is constellation prize&#32;&#91;''[[sic]]''&#93;.

The displayed page content generated by a template call (which is the rendering of the expansion of the called template) is the template result. The template result generated by the template call {{sic|constellation prize}} is "constellation prize [sic]".

The name of a template is the name of the Wikipedia page (which is also the title of the page). The namespace is normally left off if it is "Template" (which it almost always is). Furthermore, it is customary to write the name with double braces, like a template call, to emphasize that it names a template. For example, "Use the {{tm}} template to generate the trademark symbol." But don't go so far as to put a template name in the code font, to prevent confusion with an actual template call. {{tm}} is a template name, but {{tm}} is a template call.

Some template calls perform a tagging function; such a template call is often called a tag, as in, "If you are in the middle of a major edit, place an {{in use}} tag at the top of the page." This is one of many ways the term "tag" is used in Wikipedia.

A parameter is a particular piece of information that can be specified in a template call to affect the expansion of the template. A parameter has a value. A named parameter has a name; an unnamed parameter has a position. A template call specifies a parameter and the code that does that is a parameter specification. A template defines a parameter (not explicitly -- just by the template being designed to use it). The code in a template that expands to the parameter value (e.g. {{{myparam}}}) is a parameter reference.

Common variations

It is common to use "template" to refer not only to a template, but to a template call, a template result, and a template name.

For example:

  • I am going to put more detail in the {{short description}} template on this page.
  • Upon seeing an {{under construction}} template, the reader may decide to come back later.
  • There are many kinds of infoboxes. The templates all start with "Infobox".

These usages can be confusing, especially to someone less familiar with templates. Consider using more specific language.

As an alternative to using this index linked above, you can search the Template namespace using the Special:Search box below:

See also

Help pages

Mediawiki manual pages

Special pages

Other backend pages

Notes

  1. ^ Namespaces from which transclusion is not allowed are specified on a wiki by the variable $wgNonincludableNamespaces.
  2. ^ There is no way to preview the effect on the page source of substitution without saving the page
  3. ^ This does not apply if the equals sign comes within another template call or other item which the parser handles separately.
  4. ^ Again, this does not apply if it comes within another separately parsed item, such as a piped wikilink.
  5. ^ For an old version, the subtemplate tree will be constructed according to the templates' current state.
  6. ^ These are defined in the doBlockLevels function of Parser.php.
玛丽珍鞋是什么意思 属猴与什么属相最配 热感冒吃什么药 见到黑猫代表什么预兆 带鱼为什么是扁的
七年是什么婚 休学是什么意思 唇炎去药店买什么药 巧克力有什么功效与作用 肾虚吃什么中药
淋巴癌是什么 翌日什么意思 老年人总睡觉是什么原因 催供香是什么意思 孕期小腿抽筋什么原因
门可罗雀什么意思 退烧药吃多了有什么副作用 右脚踝肿是什么原因引起的 头皮痒用什么洗发水效果好 口臭严重是什么原因
蒋介石为什么不杀张学良sscsqa.com 足齐念什么hcv9jop1ns2r.cn 乳头为什么会痛inbungee.com 肩胛骨痛挂什么科jiuxinfghf.com 92属什么hcv8jop4ns2r.cn
生孩子前要注意什么hcv7jop9ns0r.cn 吃什么代谢快有助于减肥hcv8jop7ns1r.cn 月亮什么颜色hcv8jop1ns5r.cn 李世民是什么生肖hcv9jop2ns7r.cn 指甲月牙白代表什么hcv9jop1ns9r.cn
判处死刑缓期二年执行是什么意思hcv8jop0ns0r.cn 胃饱胀是什么原因hcv9jop8ns2r.cn 摩登女郎是什么意思inbungee.com 手麻木吃什么药好mmeoe.com 试纸一深一浅说明什么hcv9jop6ns2r.cn
骨髓炎是什么病hcv9jop7ns0r.cn 梦见自己孩子死了是什么意思hcv9jop6ns0r.cn 木耳不能和什么食物一起吃hcv9jop7ns0r.cn 一泻千里是什么意思hcv8jop3ns8r.cn 金玉其外败絮其中是什么意思hcv9jop5ns0r.cn
百度