Game widow (talk | contribs) No edit summary |
(+pt) |
||
(38 intermediate revisions by 17 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Ambox |
||
− | If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play. |
||
+ | |type = For a list of all the templates on this wiki, see [https://help.gamepedia.com/index.php?title=Special%3APrefixIndex&prefix=&namespace=10 this list] |
||
+ | |border = blue |
||
+ | |style = font-size:80% |
||
+ | }} |
||
+ | <br/> |
||
+ | {{Infobox |
||
+ | |name = Example infobox |
||
+ | |image = Curse hydra.png |
||
+ | |race = Plushie |
||
+ | |location = Curse Headquarters |
||
+ | |source = [[User:Encredechine{{!}}Encredechine]] |
||
+ | |related = Fun |
||
+ | }} |
||
+ | At their most basic, '''templates''' are basically just wiki pages whose content may be pulled into other pages. In other words, all the content you see when viewing a page may not actually be in the page source. Some or even all of it may be in templates. |
||
+ | Three very common types of templates are shown here as examples: |
||
− | ==Creation== |
||
+ | * At the top, there is an example of a "message box" template, usually used for article maintenance. |
||
− | Templates are standard wiki pages whose content is designed to be [[wikipedia:Wikipedia:Transclusion|transcluded]] (embedded) inside other pages. Templates follow a convention that the name is prefixed with "<code>Template:</code>", assigning it to that [[Help:Namespaces|namespace]]; besides this, you can [[Help:Starting a new page|create them like any other wiki page]]. |
||
+ | * To the right, there is an example of an "infobox" template. Nearly every wiki provides at least one of these. |
||
+ | * Below, there is an example of a navigation template called a "navbox" typically used at the bottom of pages. |
||
+ | {{Example navigation}} |
||
+ | Speaking generally, a template is any wiki page whose content is designed to be [[wikipedia:Wikipedia:Transclusion|transcluded]] (embedded) inside other pages. Templates follow a convention that the name is prefixed with <code>Template:</code>, assigning it to that [[Namespaces|namespace]]. |
||
− | The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents: |
||
+ | == Templates in the page source == |
||
− | Hello! Welcome to the wiki. |
||
+ | When editing wiki pages, it is likely you might encounter something like this: |
||
+ | <pre>{{Example}}</pre> |
||
− | you'll have created your first template! If you then insert the code: |
||
+ | Or perhaps something more complex like this: |
||
− | <nowiki>{{Welcome}}</nowiki> |
||
+ | <pre> |
||
− | in any other page, when that page is viewed the text "Hello! Welcome to the wiki." will appear instead of <code><nowiki>{{Welcome}}</nowiki></code>. The template content is "transcluded" into the other page, i.e. it is integrated in the page. |
||
+ | {{Infobox |
||
+ | |name = Example infobox |
||
+ | |image = Curse hydra.png |
||
+ | |race = Plushie |
||
+ | |location = Curse Headquarters |
||
+ | |source = [[User:Encredechine{{!}}Encredechine]] |
||
+ | |related = Fun |
||
+ | }} |
||
+ | </pre> |
||
+ | These are examples of templates being used on these wiki pages. The first is using <nowiki>[[Template:Example]]</nowiki>, the second is using <nowiki>[[Template:Infobox]]</nowiki>. The double curly brackets, <code><nowiki>{{</nowiki></code> and <code>}}</code> usually mean that a template is being used there, but unfortunately, double curly brackets also have other uses in wikitext, which can cause some confusion. |
||
− | You can then insert <code><nowiki>{{Welcome}}</nowiki></code> at any point of any page where you wish to welcome someone. Suppose it is used in 100 pages. If you then change the template contents to: |
||
+ | === How to tell if it's a template === |
||
− | Hi there! Welcome to this wonderful wiki. |
||
+ | Let's look at our examples above again. |
||
+ | <pre> |
||
− | and revisit any of the 100 pages where the template was used, you'll see the new text instead of the original one. In this way, you have changed the content of 100 pages without editing them, because the template is transcluded into these pages. |
||
+ | {{Infobox |
||
+ | |name = Example infobox |
||
+ | |image = Curse hydra.png |
||
+ | |race = Plushie |
||
+ | |location = Curse Headquarters |
||
+ | |source = [[User:Encredechine{{!}}Encredechine]] |
||
+ | |related = Fun |
||
+ | }} |
||
+ | </pre> |
||
+ | In this case, we have a definite clue because the first thing after the name other than just space is a pipe character: (<code>|</code>). This is definitely a template. If it were a colon instead, (<code>:</code>), then it would not be a template, but some kind of [[magic words|magic word]]. |
||
− | This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful. |
||
+ | <pre>{{Example}}</pre> |
||
− | ==Usage== |
||
− | Templates can be used in other pages in these ways: |
||
+ | Unfortunately, this one doesn't offer any definite clues right off the bat. If you aren't sure, there are a few ways to make a pretty good guess: |
||
− | *<code><nowiki>{{Name}}</nowiki></code> — as described above, this link will be dynamically replaced by the current content of <nowiki>[[Template:Name]]</nowiki> at the time the page with the template link is loaded. The link will remain unchanged in the page's source. |
||
+ | * One clue is that it is a template is that ''isn't'' in all capital letters. Virtually all magic words that don't require a colon are typed in all caps. |
||
− | *<code><nowiki>{{subst:Name}}</nowiki></code> — when this template link is used, it will be replaced once and for all with the content of <nowiki>[[Template:Name]]</nowiki> as of the time the page with the link is '''saved''': a copy of the contents of <nowiki>[[Template:Name]]</nowiki> will be ''substituted'' for the template link. The contents are then a part of the including page, and can be edited normally, separately from the original. ''Note'': changes to the source template page will ''not'' be propagated to the page with the template link. |
||
+ | * To be sure, when editing a page, you should see "Templates used on this page" at the bottom of the screen under the "Save Page" and other buttons. If it's listed there, it's definitely a template. |
||
− | *<code><nowiki>{{safesubst:Name}}</nowiki></code> — this is for special cases where a template may be designed to work both through transclusion and substitution. For example, say you used a template <nowiki>{{Firstname}}</nowiki> within <nowiki>[[Template:Name]]</nowiki>. If you use <nowiki>{{subst:Name}}</nowiki>, the <nowiki>{{Firstname}}</nowiki> will be saved on the page. If you try to change it to <nowiki>{{subst:Firstname}}</nowiki> inside <nowiki>[[Template:Name]]</nowiki>, then it'd be substituted inside the template. safesubst: doesn't substitute until the template containing it is used with subst:. |
||
+ | * If it isn't a documented [[magic words|magic word]], it's almost certainly a template. |
||
− | *<code><nowiki>{{msgnw:Name}}</nowiki></code> includes the template in a form that displays it as raw wiki syntax (the way <code><nowiki><nowiki></nowiki></code> does) when the page containing it is fetched. |
||
+ | == Basic transclusion == |
||
− | In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so: |
||
+ | {{See|mw:Help:Transclusion}} |
||
+ | Transclusion is by far the most common way to use templates. |
||
− | *<code><nowiki>{{ |
+ | * <code><nowiki>{{Name}}</nowiki></code> — as described above, this link will be dynamically replaced by the current content of <nowiki>[[Template:Name]]</nowiki> at the time the page with the template link is loaded. The page's source will be unchanged. |
+ | |||
− | *<code><nowiki>{{Foo:Pagename}}</nowiki></code> includes <code><nowiki>[[Foo:Pagename]]</nowiki></code> |
||
+ | In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace in which it resides, so: |
||
− | *<code><nowiki>{{:Pagename}}</nowiki></code> includes <code><nowiki>[[Pagename]]</nowiki></code> |
||
− | * |
+ | * <code><nowiki>{{Template:Pagename}}</nowiki></code> includes <code><nowiki>[[Template:Pagename]]</nowiki></code> |
+ | * <code><nowiki>{{Foo:Pagename}}</nowiki></code> includes <code><nowiki>[[Foo:Pagename]]</nowiki></code> |
||
+ | * <code><nowiki>{{:Pagename}}</nowiki></code> includes <code><nowiki>[[Pagename]]</nowiki></code> |
||
+ | ** <code><nowiki>{{SUBST::Pagename}}</nowiki></code> replaces itself with the contents of <code><nowiki>[[Pagename]]</nowiki></code> |
||
If no such namespace exists, the full title is assumed to be a template: |
If no such namespace exists, the full title is assumed to be a template: |
||
− | *<code><nowiki>{{Foo:Bar}}</nowiki></code> |
+ | * <code><nowiki>{{Foo:Bar}}</nowiki></code> includes <code><nowiki>[[Template:Foo:Bar]]</nowiki></code> |
− | |||
− | ==Parameters== |
||
− | To enrich the mechanism of transclusion, MediaWiki allows parameters to be passed to a template when it is transcluded. Parameters allow the template to produce different contents or have different behaviors. |
||
− | |||
− | Suppose you wish to insert a little thank you note in the [[Help:Talk pages|talk page]] of other users, such as: |
||
− | |||
− | <nowiki>{{Thankyou|all your effort|Me}}</nowiki> |
||
− | The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user is able to thank any other user, for any reason whatsoever. |
||
− | |||
− | So that the note will look similar everywhere it is used, you can define a template called <nowiki>[[Template:Thankyou]]</nowiki>, for example. Although the note should look similar whenever a user thanks another user, its specific contents (i.e. the reason and the signature) will be different. For that reason, you should pass them as parameters. If we ignore the remaining elements to format the box and place the image, the core content of the template will be this: |
||
− | |||
− | <pre><nowiki>'''A little thank you...''' |
||
− | for {{{1}}}. |
||
− | hugs, {{{2}}}</nowiki></pre> |
||
− | |||
− | Notice the use of <code><nowiki>{{{1}}}</nowiki></code> and <code><nowiki>{{{2}}}</nowiki></code>. This is the way to identify, within templates, the parameters that will be passed in when the template is used. Note that, within the template, each parameter is surrounded by '''three''' braces: <code><nowiki>{{{ }}}</nowiki></code>. This is different from normal template name usage. |
||
− | |||
− | When using the template on a page, you fill in the parameter values, separated by a "pipe" character (<code>|</code>). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named. |
||
− | |||
− | === Anonymous parameters === |
||
− | To pass in anonymous parameters, list the values of those parameters sequentially: |
||
− | |||
− | <nowiki>{{Thankyou|all your effort|Me}}</nowiki> |
||
− | In this case, the <code><nowiki>{{Thankyou}}</nowiki></code> template receives parameters <code><nowiki>{{{1}}}=all your effort</nowiki></code> and <code><nowiki>{{{2}}}=Me</nowiki></code>, producing: |
||
− | |||
− | '''A little thank you...''' |
||
− | for all your effort. |
||
− | hugs, Me |
||
− | |||
− | The order in which anonymous parameters are passed in is crucial to its behaviour. Reversing the order of the parameters, like so: |
||
− | |||
− | <nowiki>{{Thankyou|Me|all your effort}}</nowiki> |
||
− | |||
− | would produce this result: |
||
− | |||
− | '''A little thank you...''' |
||
− | for Me. |
||
− | hugs, all your effort |
||
− | |||
− | '''Note:''' identifying parameters by order (with <code><nowiki>{{{1}}}</nowiki></code>, etc) works ''only'' with anonymous parameters. If your page identifies any parameter by number or name, as shown below, this method will no longer be available to the template which receives them. |
||
− | |||
− | === Numbered parameters === |
||
− | To pass in parameters by number, identify each parameter when passing it: |
||
− | |||
− | <nowiki>{{Thankyou|2=Me|1=your friendship}}</nowiki> |
||
− | |||
− | This time, template <code><nowiki>{{Thankyou}}</nowiki></code> receives parameters <code><nowiki>{{{1}}}=your friendship</nowiki></code> and <code><nowiki>{{{2}}}=Me</nowiki></code>, though they have been supplied in inverse order, and produces: |
||
− | |||
− | '''A little thank you...''' |
||
− | for your friendship. |
||
− | hugs, Me |
||
− | |||
− | === Named parameters === |
||
− | The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to: |
||
− | |||
− | <pre><nowiki>'''A little thank you...''' |
||
− | for {{{reason}}}. |
||
− | hugs, {{{signature}}}</nowiki></pre> |
||
− | |||
− | Within the template, we use <code><nowiki>{{{reason}}}</nowiki></code> and <code><nowiki>{{{signature}}}</nowiki></code> to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it: |
||
− | |||
− | <nowiki>{{Thankyou|signature=Me|reason=being who you are}}</nowiki> |
||
− | |||
− | In this case, template <code><nowiki>{{Thankyou}}</nowiki></code> receives parameters <code><nowiki>{{{reason}}}=being who you are</nowiki></code> and <code><nowiki>{{{signature}}}=Me</nowiki></code> and produces: |
||
− | |||
− | '''A little thank you...''' |
||
− | for being who you are. |
||
− | hugs, Me |
||
− | |||
− | The advantage of using named parameters in your template, besides also being flexible in the order parameters can be passed, is that it makes the template code much easier to understand if there are many parameters. |
||
− | |||
− | === Default values === |
||
− | If you transclude a template that expects parameters, but do not provide them, in this way: |
||
− | |||
− | <nowiki>{{Thankyou}}</nowiki> |
||
− | |||
− | in the numbered parameters example above you would get the following: |
||
− | |||
− | '''A little thank you...''' |
||
− | for {{{1}}}. |
||
− | hugs, {{{2}}} |
||
− | |||
− | Since no parameters were passed in, the template presents the parameters themselves, instead of their respective values. In these cases, it may be useful to define ''default'' values for the parameters, i.e. values that will be used if no value is passed in. For example, if the template contents are changed to: |
||
− | |||
− | <pre><nowiki>'''A little thank you...''' |
||
− | for {{{reason|everything}}}. |
||
− | hugs, {{{signature|Me}}}</nowiki></pre> |
||
− | |||
− | then <code><nowiki>{{{reason|everything}}}</nowiki></code> defines that if no parameter <code>{{{reason}}}</code> is provided, then the value <code>everything</code> will be used. Similarly, <code><nowiki>{{{signature|Me}}}</nowiki></code>, defaults parameter <code>{{{signature}}}</code> to value <code>Me</code>. Now, transcluding the template again without passing any parameter, results in the following: |
||
− | <!-- strictly speaking, this example is false, because the template call was changed to obtain the desired effect - but the effect is the same if the template is defined as described --> |
||
− | |||
− | '''A little thank you...''' |
||
− | for everything. |
||
− | hugs, Me |
||
− | |||
− | ==Control template inclusion== |
||
− | By default, a template's content is displayed in its entirety, both when viewed directly and when included in another page. However, you can control which parts of a template will be seen and included by the use of the <code><nowiki><noinclude></nowiki></code>, <code><nowiki><includeonly></nowiki></code>, and <code><nowiki><onlyinclude></nowiki></code> tags. |
||
− | |||
− | ===Showing content only on the template page=== |
||
− | Anything between <code><nowiki><noinclude></nowiki></code> and <code><nowiki></noinclude></nowiki></code> will be seen only when the template's page is being viewed directly, but not when it is included in another page. This is useful when you want to include text or code in a template that you do not want to propagate to any pages which include it, such as: |
||
− | |||
− | * [[Help:Categories|Category]] links when categorizing the template itself |
||
− | * [[Help:Links#Interlanguage links|Interlanguage links]] to similar templates in other languages |
||
− | * Explanatory text about how to use the template |
||
− | |||
− | Alternatively, anything ''outside'' <code><nowiki><onlyinclude></nowiki></code> and <code><nowiki></onlyinclude></nowiki></code> is seen only when the template's page is viewed directly. <code><nowiki><onlyinclude></nowiki></code> and <code><nowiki><noinclude></nowiki></code> have the same purpose, but function in opposite ways. |
||
− | |||
− | ===Showing content only on transclusion or substitution=== |
||
− | <code><nowiki><onlyinclude></nowiki></code> is not to be confused with <code><nowiki><includeonly></nowiki></code>, which has a different purpose. Anything between <code><nowiki><includeonly></nowiki></code> and <code><nowiki></includeonly></nowiki></code> will be processed and displayed only when the page is being included, but not when the template page is being viewed directly, and is useful in situations such as: |
||
− | |||
− | * [[Help:Categories| Categorizing]] pages which include the template. ''Note:'' when changing the categories applied by a template in this fashion, the categorization of the pages which include that template may not be updated until some time later: this is handled by the job queue. To force the re-categorization of a particular page, open that page for editing and save it without changes. |
||
− | * Ensuring that the template's code is not executed when viewing the template page itself. Typically this is because it expects parameters, and its execution without parameters has an undesired result. |
||
− | |||
− | ===Always showing content=== |
||
− | Unless <code><nowiki><onlyinclude></nowiki></code> tags are used, everything outside <code><nowiki><noinclude></nowiki></code> and <code><nowiki><includeonly></nowiki></code> tags is processed and displayed normally; that is, both when the template page is being viewed directly and when the template is included in another page. |
||
− | |||
− | Otherwise, anything inside <code><nowiki><onlyinclude></nowiki></code> tags will be shown both on the template page and when included, unless <code><nowiki><includeonly></nowiki></code> tags are used within. |
||
− | |||
− | ==Organizing templates== |
||
− | For templates to be effective, users need to find them, and find out how to use them. |
||
− | |||
− | To find them, users can: |
||
− | # Click '''Special Pages > All Pages''' |
||
− | # In the '''Namespace''' list, choose '''Template''' and click '''Go'''. |
||
− | |||
− | To give usage information, include an example like this one on the template page: |
||
− | |||
− | <pre><nowiki> |
||
− | <noinclude> |
||
− | == Usage == |
||
− | Welcome users: |
||
− | {{Thankyou|reason=your reason|signature=your signature}} |
||
− | </noinclude> |
||
− | </nowiki></pre> |
||
− | |||
− | Then, an editor can simply copy and paste the example to use the template. |
||
+ | There are other ways of using templates, but these are useful only in special situations. Most editors will never have any use for these special functions, so they are explained only at the [[#Alternate ways to use templates|bottom of this page]]. |
||
− | ==Copying from one wiki to another== |
||
− | Templates often require [http://meta.wikimedia.org/wiki/Help:CSS CSS] or other templates, so users frequently have trouble copying templates from one wiki to another. The steps below should work for most templates. |
||
− | === |
+ | === Template documentation === |
+ | Many template authors provide documentation for how to use specific templates. This can usually be found on the template page itself. If you are editing a page using a particular template, one way you can usually get to the template page quickly is by using the "Templates used on this page" section at the bottom of the edit page. |
||
− | If you have import rights on the new wiki: |
||
− | # Go to [[Special:Export]] on the original wiki, and download an .xml file with the complete history of all necessary templates, as follows: |
||
− | #* Enter the name of the template in the big text box, e.g. "Template:Welcome". Pay special attention to capitalization and special characters — if the template name isn't exactly correct, the export may still occur but the .xml file will not have the expected data. |
||
− | #* Check the box "Include templates". |
||
− | #* '''Uncheck''' the box "Include only the current revision". |
||
− | #* Click Export. |
||
− | # Go to [[Special:Import]] on the new wiki and upload the .xml file. |
||
+ | Unfortunately, many templates are not documented. In this case, you can try looking for examples of the template being used on other pages. From the template page, try clicking "[[What links here]]" in the "Tools" menu in the sidebar to find other pages where the template is used. If it's not documented, consider writing your own template documentation. |
||
− | If you don't have import rights on the new wiki: |
||
− | # Go to [[Special:Export]] on the original wiki, and download an .xml file with the latest version only of all necessary templates, as follows: |
||
− | #* Enter the name of the template in the big text box. |
||
− | #* Check the box "Include templates". |
||
− | #* '''Check''' the box "Include only the current revision". |
||
− | #* Click Export. |
||
− | #* Open the file in a text editor and replace certain [[wikipedia:en:XML entity|XML entities]] with the corresponding characters: &lt; → <, &gt; → >, &quot; → " and &amp; → &. Because of XML syntax rules, these entities appear in the XML file, but they should not appear in the edit box of the MediaWiki instance. |
||
− | #* Manually copy the text inside the <code><nowiki><text></nowiki></code> tag of each listed template into a similarly named template in your wiki. In the edit summary of each template, link to the original page for attribution. |
||
+ | It can help to understand how to [[Writing templates|write templates]]. Once you do, you may be able to figure out a template simply by opening it for editing and looking at its source, but templates can be very complex and may use advanced wiki features or specific extensions, like any other wiki page. If all else fails, never hesitate to ask for assistance from the wiki community. |
||
− | This will copy the entire code necessary, and will suffice for some templates. |
||
+ | == Alternate ways to use templates == |
||
− | === Extensions === |
||
+ | In addition to standard transclusion, templates can be used in other pages in these ways: |
||
− | An extension often used in templates is ParserFunctions. Visit page [[mw:Help:Extension:ParserFunctions|ParserFunctions]] and check if any of the functions listed there are used in the templates you've copied. The [[mw:Extension:ParserFunctions|ParserFunctions extension]] is installed by default on Gamepedia wikis. |
||
+ | * <code><nowiki>{{subst:Name}}</nowiki></code> — when this template link is used, it will be replaced once and for all with the content of <nowiki>[[Template:Name]]</nowiki> as of the time the page with the link is '''saved'''; a copy of the contents of <nowiki>[[Template:Name]]</nowiki> will be ''substituted'' for the template link. The contents are then a part of the including page, and can be edited normally, separately from the original. Changes to the source template page will ''not'' be propagated to the page with the template link. |
||
− | === CSS and JavaScript code === |
||
+ | * <code><nowiki>{{safesubst:Name}}</nowiki></code> — this is for special cases where a template may be designed to work both through transclusion and substitution. For example, say you used a template <nowiki>{{Firstname}}</nowiki> within <nowiki>[[Template:Name]]</nowiki>. If you use <nowiki>{{subst:Name}}</nowiki>, the <nowiki>{{Firstname}}</nowiki> will be saved on the page. If you try to change it to <nowiki>{{subst:Firstname}}</nowiki> inside <nowiki>[[Template:Name]]</nowiki>, then it'd be substituted inside the template. <code>safesubst:</code> doesn't substitute until the template containing it is used with <code>subst:</code>. |
||
− | Besides MediaWiki code, many templates make use of CSS and some rely on JavaScript to work fully. If the copied templates are not behaving as expected, this may be the cause. To copy the required CSS and JavaScript to your wiki you'll normally need to have admin priviledges, because you'll be editing system messages in the "MediaWiki:" namespace. |
||
+ | * <code><nowiki>{{msgnw:Name}}</nowiki></code> includes the template in a form that displays it as raw wiki syntax (the way <code><nowiki><nowiki></nowiki></code> does) when the page containing it is fetched. |
||
+ | == See also == |
||
− | # Look for the use of CSS classes (text like <code>class="foobar"</code>) in the template text. If those classes appear in "MediaWiki:Common.css" or "MediaWiki:Monobook.css" on the original wiki, copy them to "MediaWiki:Common.css" on the new wiki and check if the template is now fine. |
||
+ | * [[wikipedia:Help:A quick guide to templates|Wikipedia's quick guide to templates]] |
||
− | # If the copied template is still not working as expected, check if there is code in "MediaWiki:Common.js" or "MediaWiki:Monobook.js" on the original wiki. If so, you can try copying it to "MediaWiki:Common.js" on the new wiki. Normally, it is a good idea to only copy code from trusted sources, and first browsing the code to identify and select the parts that are relevant. You may find comments that can serve as clues to identify the functionality of each part. |
||
+ | [[Category:Editing]] |
||
− | ==See also== |
||
+ | [[Category:Templates| ]] |
||
− | *[[Help:Magic words]] – fancy stuff you may find in some templates |
||
+ | [[fr:Modèles]] |
||
− | [[Category:Advanced editing]] |
||
+ | [[pl:Szablony]] |
||
+ | [[pt:Predefinições]] |
||
+ | [[tr:Şablonlar]] |
Revision as of 14:58, 6 April 2021
For a list of all the templates on this wiki, see this list |
At their most basic, templates are basically just wiki pages whose content may be pulled into other pages. In other words, all the content you see when viewing a page may not actually be in the page source. Some or even all of it may be in templates.
Three very common types of templates are shown here as examples:
- At the top, there is an example of a "message box" template, usually used for article maintenance.
- To the right, there is an example of an "infobox" template. Nearly every wiki provides at least one of these.
- Below, there is an example of a navigation template called a "navbox" typically used at the bottom of pages.
|
Speaking generally, a template is any wiki page whose content is designed to be transcluded (embedded) inside other pages. Templates follow a convention that the name is prefixed with Template:
, assigning it to that namespace.
Templates in the page source
When editing wiki pages, it is likely you might encounter something like this:
{{Example}}
Or perhaps something more complex like this:
{{Infobox |name = Example infobox |image = Curse hydra.png |race = Plushie |location = Curse Headquarters |source = [[User:Encredechine{{!}}Encredechine]] |related = Fun }}
These are examples of templates being used on these wiki pages. The first is using [[Template:Example]], the second is using [[Template:Infobox]]. The double curly brackets, {{
and }}
usually mean that a template is being used there, but unfortunately, double curly brackets also have other uses in wikitext, which can cause some confusion.
How to tell if it's a template
Let's look at our examples above again.
{{Infobox |name = Example infobox |image = Curse hydra.png |race = Plushie |location = Curse Headquarters |source = [[User:Encredechine{{!}}Encredechine]] |related = Fun }}
In this case, we have a definite clue because the first thing after the name other than just space is a pipe character: (|
). This is definitely a template. If it were a colon instead, (:
), then it would not be a template, but some kind of magic word.
{{Example}}
Unfortunately, this one doesn't offer any definite clues right off the bat. If you aren't sure, there are a few ways to make a pretty good guess:
- One clue is that it is a template is that isn't in all capital letters. Virtually all magic words that don't require a colon are typed in all caps.
- To be sure, when editing a page, you should see "Templates used on this page" at the bottom of the screen under the "Save Page" and other buttons. If it's listed there, it's definitely a template.
- If it isn't a documented magic word, it's almost certainly a template.
Basic transclusion
Transclusion is by far the most common way to use templates.
{{Name}}
— as described above, this link will be dynamically replaced by the current content of [[Template:Name]] at the time the page with the template link is loaded. The page's source will be unchanged.
In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace in which it resides, so:
{{Template:Pagename}}
includes[[Template:Pagename]]
{{Foo:Pagename}}
includes[[Foo:Pagename]]
{{:Pagename}}
includes[[Pagename]]
{{SUBST::Pagename}}
replaces itself with the contents of[[Pagename]]
If no such namespace exists, the full title is assumed to be a template:
{{Foo:Bar}}
includes[[Template:Foo:Bar]]
There are other ways of using templates, but these are useful only in special situations. Most editors will never have any use for these special functions, so they are explained only at the bottom of this page.
Template documentation
Many template authors provide documentation for how to use specific templates. This can usually be found on the template page itself. If you are editing a page using a particular template, one way you can usually get to the template page quickly is by using the "Templates used on this page" section at the bottom of the edit page.
Unfortunately, many templates are not documented. In this case, you can try looking for examples of the template being used on other pages. From the template page, try clicking "What links here" in the "Tools" menu in the sidebar to find other pages where the template is used. If it's not documented, consider writing your own template documentation.
It can help to understand how to write templates. Once you do, you may be able to figure out a template simply by opening it for editing and looking at its source, but templates can be very complex and may use advanced wiki features or specific extensions, like any other wiki page. If all else fails, never hesitate to ask for assistance from the wiki community.
Alternate ways to use templates
In addition to standard transclusion, templates can be used in other pages in these ways:
{{subst:Name}}
— when this template link is used, it will be replaced once and for all with the content of [[Template:Name]] as of the time the page with the link is saved; a copy of the contents of [[Template:Name]] will be substituted for the template link. The contents are then a part of the including page, and can be edited normally, separately from the original. Changes to the source template page will not be propagated to the page with the template link.{{safesubst:Name}}
— this is for special cases where a template may be designed to work both through transclusion and substitution. For example, say you used a template {{Firstname}} within [[Template:Name]]. If you use {{subst:Name}}, the {{Firstname}} will be saved on the page. If you try to change it to {{subst:Firstname}} inside [[Template:Name]], then it'd be substituted inside the template.safesubst:
doesn't substitute until the template containing it is used withsubst:
.{{msgnw:Name}}
includes the template in a form that displays it as raw wiki syntax (the way<nowiki>
does) when the page containing it is fetched.