「ヘルプ:Templates」の版間の差分

提供: ひつじ小屋別館2代目
移動先: 案内検索
(1版)
(1版 をインポートしました)
 
(8人の利用者による、間の25版が非表示)
1行目: 1行目:
{{PD Help Page}}
+
<languages />
If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play.
+
{{TNT|PD Help Page}}
 +
<translate>
 +
<!--T:1-->
 +
If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play.</translate>
 +
<translate>
 +
<!--T:116-->
 +
Unlike extensions and media files, there is no central repository for templates. Templates can be newly written or to save duplicating work already done, exported from another wiki e.g. Wikipedia, and then imported into the target wiki.
  
==Creation==
+
== Creation == <!--T:2-->
Templates are standard wiki pages whose content is designed to be [[w: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]].
 
  
 +
<!--T:3-->
 +
Templates are standard wiki pages whose content is designed to be [[Special:MyLanguage/Transclusion|transcluded]] (embedded) inside other pages. Templates follow a convention that the name is prefixed with "<code>Template:</code>", assigning it to that [[Special:MyLanguage/Help:Namespaces|namespace]]; besides this, you can [[Special:MyLanguage/Help:Starting a new page|create them like any other wiki page]].
 +
 +
<!--T:4-->
 
The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents:
 
The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents:
  
  Hello! Welcome to the wiki.
+
  <!--T:5-->
 +
Hello! Welcome to the wiki.
  
 +
<!--T:6-->
 
you'll have created your first template! If you then insert the code:
 
you'll have created your first template! If you then insert the code:
  
  <nowiki>{{Welcome}}</nowiki>
+
  <!--T:7-->
 +
<nowiki>{{Welcome}}</nowiki>
  
 +
<!--T:8-->
 
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.
 
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.
  
 +
<!--T:9-->
 
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:
 
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:
  
  Hi there! Welcome to this wonderful wiki.
+
  <!--T:10-->
 +
Hi there! Welcome to this wonderful wiki.
  
 +
<!--T:11-->
 
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.
 
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.
  
 +
<!--T:12-->
 
This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.
 
This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.
  
==Usage==
+
== Usage == <!--T:13-->
 +
 
 +
<!--T:14-->
 
Templates can be used in other pages in these ways:
 
Templates can be used in other pages in these ways:
  
*<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.
+
<!--T:15-->
*<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.
+
* <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.
*<code><nowiki>{{safesubst:Name}}</nowiki></code> — this was introduced in [[rev:61710]] to allow for substitution that doesn't break transclusion, see [[w:en:Help:Substitution#The safesubst: modifier]].
+
</translate>
*<code><nowiki>{{msgnw:Name}}</nowiki></code> includes the template in a form that displays it as raw wiki syntax (the way <code><nowiki>&lt;nowiki&gt;</nowiki></code> does) when the page containing it is fetched.
+
<translate>
 +
<!--T:16-->
 +
* <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'': later changes to the source template page will ''not'' be propagated to the page with the template link.
 +
</translate>
 +
<translate>
 +
<!--T:17-->
 +
* <code><nowiki>{{safesubst:Name}}</nowiki></code> — this was introduced to allow for substitution that doesn't break transclusion, see [[w:en:Help:Substitution#The safesubst: modifier]].
 +
</translate>
 +
<translate>
 +
<!--T:18-->
 +
* <code><nowiki>{{msgnw:Name}}</nowiki></code> includes the template in a form that displays it as raw wiki syntax (the way <code><nowiki>&lt;nowiki&gt;</nowiki></code> does) when the page containing it is fetched.
  
 +
<!--T:19-->
 
In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so:
 
In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so:
  
*<code><nowiki>{{Template:Pagename}}</nowiki></code> includes <code><nowiki>[[Template:Pagename]]</nowiki></code>
+
<!--T:20-->
*<code><nowiki>{{Foo:Pagename}}</nowiki></code>       includes <code><nowiki>[[Foo:Pagename]]</nowiki></code>
+
* <code><nowiki>{{Template:Pagename}}</nowiki></code> includes <code><nowiki>[[Template:Pagename]]</nowiki></code>
*<code><nowiki>{{:Pagename}}</nowiki></code>         includes <code><nowiki>[[Pagename]]</nowiki></code>
+
</translate>
**<code><nowiki>{{subst::Pagename}}</nowiki></code>   replaces itself with the contents of <code><nowiki>[[Pagename]]</nowiki></code>
+
<translate>
 +
<!--T:21-->
 +
* <code><nowiki>{{Foo:Pagename}}</nowiki></code> includes <code><nowiki>[[Foo:Pagename]]</nowiki></code>
 +
</translate>
 +
<translate>
 +
<!--T:22-->
 +
* <code><nowiki>{{:Pagename}}</nowiki></code> includes <code><nowiki>[[Pagename]]</nowiki></code>
 +
</translate>
 +
<translate>
 +
<!--T:23-->
 +
** <code><nowiki>{{subst::Pagename}}</nowiki></code> replaces itself with the contents of <code><nowiki>[[Pagename]]</nowiki></code>
  
 +
<!--T:24-->
 
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>           includes <code><nowiki>[[Template:Foo:Bar]]</nowiki></code>
+
<!--T:25-->
 +
* <code><nowiki>{{Foo:Bar}}</nowiki></code> includes <code><nowiki>[[Template:Foo:Bar]]</nowiki></code>
  
==Parameters==
+
== Parameters == <!--T:26-->
 +
 
 +
<!--T:27-->
 
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.
 
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:
+
<!--T:28-->
 +
Suppose you wish to insert a little thank you note in the [[Special:MyLanguage/Help:Talk pages|talk page]] of other users, such as:
  
 +
<!--T:29-->
 
{{Thankyou|all your effort|Me}}
 
{{Thankyou|all your effort|Me}}
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.
 
  
 +
<!--T:30-->
 +
The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user be able to thank any other user, for any reason whatsoever.
 +
 +
<!--T:31-->
 
So that the note will look similar everywhere it is used, you can define a template called [[Template:Thankyou]], 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:
 
So that the note will look similar everywhere it is used, you can define a template called [[Template:Thankyou]], 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:
 +
</translate>
  
<pre><nowiki>'''A little thank you...'''
+
<source lang="moin">
 +
<translate><!--T:32-->
 +
'''A little thank you...'''
 
for {{{1}}}.
 
for {{{1}}}.
hugs, {{{2}}}</nowiki></pre>
+
hugs, {{{2}}}</translate>
 +
</source>
 +
 
 +
<translate>
 +
<!--T:33-->
 +
Notice the use of <tvar|1><code><nowiki>{{{1}}}</nowiki></code></> and <tvar|2><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: <tvar|braces><code><nowiki>{{{ }}}</nowiki></code></>. This is different from normal template name usage.
  
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.
+
<!--T:34-->
 +
When using the template on a page, you fill in the parameter values, separated by a "pipe" character (<tvar|pipe><code>|</code></>). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named.
  
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 === <!--T:35-->
  
=== Anonymous parameters ===
+
<!--T:36-->
 
To pass in anonymous parameters, list the values of those parameters sequentially:
 
To pass in anonymous parameters, list the values of those parameters sequentially:
  
  <nowiki>{{Thankyou|all your effort|Me}}</nowiki>
+
  <!--T:37-->
 +
<nowiki>{{Thankyou|all your effort|Me}}</nowiki>
 +
 
 +
<!--T:38-->
 
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:
 
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:
  
 +
<!--T:39-->
 
{{Thankyou|all your effort|Me}}
 
{{Thankyou|all your effort|Me}}
  
The order in which anonymous parameters are passed in is crucial to its behaviour. Reversing the order of the parameters, like so:
+
<!--T:40-->
 +
The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:
  
  <nowiki>{{Thankyou|Me|all your effort}}</nowiki>
+
  <!--T:41-->
 +
<nowiki>{{Thankyou|Me|all your effort}}</nowiki>
  
 +
<!--T:42-->
 
would produce this result:
 
would produce this result:
  
 +
<!--T:43-->
 
{{Thankyou|Me|all your effort}}
 
{{Thankyou|Me|all your effort}}
  
 +
<!--T:44-->
 
'''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.
 
'''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 ===
+
<!--T:120-->
 +
'''Note:''' If an equal sign appears inside an anonymous template parameter, it may be interpreted as a named parameter, treating the text before the equal sign as the parameter name and the text after it as the value.</translate>
 +
<translate>
 +
<!--T:121-->
 +
This is a common problem when you need to include an external link, or an HTML element with attributes (see [[<tvar|task>phab:T16235</>|Task 16235]])</translate>
 +
<translate>
 +
<!--T:122-->
 +
The workaround is to use named parameters instead, or even numbered parameters as explained in the following section.
 +
 
 +
=== Numbered parameters === <!--T:45-->
 +
 
 +
<!--T:46-->
 
To pass in parameters by number, identify each parameter when passing it:
 
To pass in parameters by number, identify each parameter when passing it:
  
  <nowiki>{{Thankyou|2=Me|1=your friendship}}</nowiki>
+
  <!--T:47-->
 +
<nowiki>{{Thankyou|2=Me|1=your friendship}}</nowiki>
  
 +
<!--T:48-->
 
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:
 
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:
 
{{Thankyou|2=Me|1=your friendship}}
 
{{Thankyou|2=Me|1=your friendship}}
  
=== Named parameters ===
+
=== Named parameters === <!--T:49-->
 +
 
 +
<!--T:50-->
 
The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:
 
The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:
  
 +
<!--T:51-->
 
<pre><nowiki>'''A little thank you...'''
 
<pre><nowiki>'''A little thank you...'''
 
for {{{reason}}}.
 
for {{{reason}}}.
 
hugs, {{{signature}}}</nowiki></pre>
 
hugs, {{{signature}}}</nowiki></pre>
  
 +
<!--T:52-->
 
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:
 
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>
+
  <!--T:53-->
 +
<nowiki>{{Thankyou|signature=Me|reason=being who you are}}</nowiki>
  
 +
<!--T:54-->
 
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:
 
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:
  
 +
<!--T:55-->
 
{{Thankyou|signature=Me|reason=being who you are}}
 
{{Thankyou|signature=Me|reason=being who you are}}
 
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.
 
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 ===
+
=== Default values === <!--T:56-->
 +
 
 +
<!--T:57-->
 
If you transclude a template that expects parameters, but do not provide them, in this way:
 
If you transclude a template that expects parameters, but do not provide them, in this way:
  
  <nowiki>{{Thankyou}}</nowiki>
+
  <!--T:58-->
 +
<nowiki>{{Thankyou}}</nowiki>
  
 +
<!--T:59-->
 
in the numbered parameters example above you would get the following:
 
in the numbered parameters example above you would get the following:
  
 +
<!--T:60-->
 
{{Thankyou}}
 
{{Thankyou}}
 +
 +
<!--T:61-->
 
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:
 
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:
 +
</translate>
  
<pre><nowiki>'''A little thank you...'''
+
<source lang="moin">
 +
<translate><!--T:62-->
 +
'''A little thank you...'''
 
for {{{reason|everything}}}.
 
for {{{reason|everything}}}.
hugs, {{{signature|Me}}}</nowiki></pre>
+
hugs, {{{signature|Me}}}</translate>
 +
</source>
  
 +
<translate>
 +
<!--T:63-->
 
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:
 
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:
 +
</translate>
 
<!-- 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 -->
 
<!-- 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 -->
 +
<translate>
 +
<!--T:146-->
 
{{Thankyou|reason=everything|signature=Me}}
 
{{Thankyou|reason=everything|signature=Me}}
  
==Control template inclusion==
+
=== Order of evaluation === <!--T:64-->
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> and
+
Template parameters are fully evaluated before they are passed to the template.
<code><nowiki><includeonly></nowiki></code> tags.
+
So if one of your parameters is a call to another template, what is
 +
actually passed is the expanded parameter.
  
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:
+
<!--T:138-->
 +
<pre><nowiki>{{Thankyou|reason=visiting ({{Welcome}})|signature=Me}}</nowiki></pre>
 +
becomes
 +
<pre><nowiki>{{Thankyou|reason=visiting (Hi there! Welcome to this wonderful wiki.)|signature=Me}}</nowiki></pre>
 +
before it becomes:
  
* [[Help:Categories|Category]] links when categorizing the template itself
+
<!--T:139-->
* [[Help:Links#Interlanguage links|interlanguage links]] to similar templates in other languages
+
{{Thankyou|reason=visiting (Hi there! Welcome to this wonderful wiki.)|signature=Me}}
 +
<!-- Can't use the actual {{Welcome}} as it's defined differently than the example definition given at the start of this page. -->
 +
 
 +
<!--T:140-->
 +
In a few circumstances, this order of evaluation makes a difference
 +
from the alternative.  (The alternative being to check the results
 +
of the template call to see if there are additional templates to be
 +
expanded.)
 +
 
 +
<!--T:141-->
 +
First, the name of the template can be expanded at the same time and
 +
in the same way as the parameters.
 +
 
 +
<!--T:142-->
 +
Thus
 +
<pre><nowiki>{{Thankyou in {{preferred language}}|signature=Me}}</nowiki></pre>
 +
might expand to
 +
<pre><nowiki>{{Thankyou in french|signature=Me}}</nowiki></pre>
 +
So that a template called <code>Thankyou in french</code> could display the thank you in the preferred language (however that was determined.)
 +
 
 +
<!--T:143-->
 +
Secondly, the template being called can't construct a template from pieces
 +
to be called later.
 +
A template that took the form
 +
<code><nowiki>{{start of call}}{{end of call}}</nowiki></code>
 +
would not work
 +
if
 +
<code>start of call</code> was <code><nowiki>{{Thankyou|</nowiki></code>
 +
and
 +
<code>end of call</code> was <code><nowiki>signature=Me}}</nowiki></code>.
 +
 
 +
<!--T:144-->
 +
Thirdly, the parameter being passed is expanded only once, rather than
 +
every place the parameter is used in the template.  This is more
 +
efficient on the mediawiki server.
 +
 
 +
<!--T:145-->
 +
Finally, the template being called can't stop the parameter from being
 +
expanded altogether.  Even if a template only uses a parameter in some
 +
circumstances, it will always have been expanded before the call was made.
 +
 
 +
== Control template inclusion == <!--T:65-->
 +
 
 +
<!--T:66-->
 +
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 <tvar|noinclude><code><nowiki><noinclude></nowiki></code></> and <tvar|includeonly><code><nowiki><includeonly></nowiki></code></> tags.
 +
 
 +
<!--T:67-->
 +
Anything between <tvar|noinclude_start><code><nowiki><noinclude></nowiki></code></> and <tvar|noinclude_end><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:
 +
 
 +
<!--T:68-->
 +
* [[Special:MyLanguage/Help:Categories|Category]] links when categorizing the template itself
 +
</translate>
 +
<translate>
 +
<!--T:69-->
 +
* [[Special:MyLanguage/Help:Links#Interlanguage links|interlanguage links]] to similar templates in other languages
 +
</translate>
 +
<translate>
 +
<!--T:70-->
 
* Explanatory text about how to use the template
 
* Explanatory text about how to use the template
  
Likewise, 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:
+
<!--T:71-->
 +
Likewise, anything between <tvar|includeonly_start><code><nowiki><includeonly></nowiki></code></> and <tvar|includeonly_end><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 {{mediawiki|Manual:Job queue|job queue}}. To force the re-categorization of a particular page, open that page for editing and save it without changes.
+
<!--T:72-->
 +
* [[Special:MyLanguage/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 {{mediawiki|Manual:Job queue|job queue}}. To force the re-categorization of a particular page, open that page for editing and save it without changes.
 +
</translate>
 +
<translate>
 +
<!--T:73-->
 
* 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.
 
* 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.
  
Everything outside <code><nowiki><noinclude></nowiki></code> and
+
<!--T:74-->
<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.
+
Everything outside <tvar|noinclude><code><nowiki><noinclude></nowiki></code></> and <tvar|includeonly><code><nowiki><includeonly></nowiki></code></> 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.</translate>
 +
<translate>
 +
<!--T:127-->
 +
The focus is on what is inside these two tags.
  
==Organizing templates==
+
<!--T:128-->
 +
Everything outside <tvar|onlyinclude><code><nowiki><onlyinclude></nowiki></code></> tags is discarded in the transclusion.</translate>
 +
<translate>
 +
<!--T:129-->
 +
Even sections tagged includeonly are discarded in the transclusion unless they are also tagged as onlyinclude.</translate>
 +
<translate>
 +
<!--T:130-->
 +
The focus is on what is outside this tag.
 +
 
 +
<!--T:131-->
 +
The three partial transclusion tags enable all possible combinations of what is processed and rendered.</translate>
 +
<translate>
 +
<!--T:132-->
 +
Comments also fill a role.
 +
 
 +
== Organizing templates == <!--T:75-->
 +
 
 +
<!--T:76-->
 
For templates to be effective, users need to find them, and find out how to use them.  
 
For templates to be effective, users need to find them, and find out how to use them.  
  
 +
<!--T:77-->
 
To find them, users can:
 
To find them, users can:
# Click '''Special Pages > All Pages'''
+
# Click '''<tvar|special>{{int|specialpages}}</>''' > '''<tvar|all>{{int|allpages}}</>'''
# In the '''Namespace''' list, choose '''Template''' and click '''Go'''.
+
# In the '''<tvar|ns>{{int|namespace}}</>''' list, choose '''Template''' and click '''<tvar|submit>{{int|allpagessubmit}}</>'''.
  
 +
<!--T:78-->
 
To give usage information, include an example like this one on the template page:
 
To give usage information, include an example like this one on the template page:
  
 
<pre><nowiki>
 
<pre><nowiki>
 
<noinclude>
 
<noinclude>
== Usage ==
+
== Usage == <!--T:79-->
 
Welcome users:
 
Welcome users:
 
{{Thankyou|reason=your reason|signature=your signature}}
 
{{Thankyou|reason=your reason|signature=your signature}}
155行目: 347行目:
 
</nowiki></pre>
 
</nowiki></pre>
  
 +
<!--T:80-->
 
Then, an editor can simply copy and paste the example to use the template.
 
Then, an editor can simply copy and paste the example to use the template.
  
==Copying from one wiki to another==
+
== Linking to a template == <!--T:133-->
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.
+
 
 +
<!--T:134-->
 +
To link to a template instead of including it, use the {{tl|tl}} template.</translate>
 +
<translate>
 +
<!--T:135-->
 +
The name of the template the editor wants to link to goes after the vertical pipe.</translate>
 +
<translate>
 +
<!--T:136-->
 +
For example:</translate>
 +
<translate>
 +
<!--T:137-->
 +
<tvar|code><code><nowiki>{{tl|navbar}}</nowiki></code></> links to the <tvar|navbar>{{tl|navbar}}</> template.
 +
 
 +
== Copying from one wiki to another == <!--T:81-->
 +
 
 +
<!--T:82-->
 +
Templates often require [[m: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.
  
=== MediaWiki code ===
+
=== MediaWiki code === <!--T:83-->
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:
+
<!--T:84-->
 +
If you have import rights (Specifically importupload) on the new wiki:
 +
</translate>
 +
<translate>
 +
<!--T:85-->
 +
# Go to <tvar|export>[[Special:Export]]</> on the original wiki, and download an .xml file with the complete history of all necessary templates, as follows:
 +
</translate>
 +
<translate>
 +
<!--T:86-->
 
#* 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.
 
#* 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".
+
</translate>
#* '''Uncheck''' the box "Include only the current revision".
+
<translate>
#* Click Export.
+
<!--T:87-->
 +
#* Check the box "<tvar|export>{{int|export-templates}}</>".
 +
</translate>
 +
<translate>
 +
<!--T:88-->
 +
#* '''Uncheck''' the box "<tvar|export2>{{int|exportcuronly}}</>".
 +
</translate>
 +
<translate>
 +
<!--T:89-->
 +
#* Click "<tvar|submit>{{int|export-submit}}</>".
 +
</translate>
 +
<translate>
 +
<!--T:90-->
 
# Go to [[Special:Import]] on the new wiki and upload the .xml file.
 
# Go to [[Special:Import]] on the new wiki and upload the .xml file.
  
 +
<!--T:91-->
 
If you don't have import rights on the new wiki:
 
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 [[w:en:XML entity|XML entities]] with the corresponding characters: &amp;lt; → <, &amp;gt; → >, &amp;quot; → " and &amp;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.
 
  
This will copy the entire code necessary, and will suffice for some templates.
+
<!--T:114-->
 +
# Go to the template you want to copy from the original wiki. Go to the edit page, and copy all the wikitext
 +
# On the new wiki, go to the page with the same name as the template you copied. Hit create/edit and paste the wikitext you copied. In the edit summary of each template, link to the original page for attribution.
 +
# Back in the original wiki at the edit window, below the edit box, look at the list of "Templates used on this page". For each template listed follow these instructions. Also do that for any template used by any of these templates, and so on.
  
=== Extensions ===
+
<!--T:115-->
 +
This will copy the entire code necessary, and will suffice for some templates.</translate>
 +
<translate>
 +
<!--T:117-->
 +
If it doesn't work also check for red links listed under "Pages transcluded onto the current version of this page:", below the edit box. If there are any repeat the above steps for these as well.
 +
 
 +
<!--T:118-->
 +
After successfully importing the template and all its linked templates from the other wiki, edit it to change customisations to suit your wiki. For example to change a logo, remove redundant categories or red links.
 +
 
 +
=== Extensions === <!--T:100-->
 +
 
 +
<!--T:101-->
 
An extension often used in templates is ParserFunctions. Visit page {{mediawiki|Help:Extension:ParserFunctions|ParserFunctions}} and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the {{mediawiki|Extension:ParserFunctions|ParserFunctions extension}}. To install it, you'll need system admin access to the server of your MediaWiki installation.
 
An extension often used in templates is ParserFunctions. Visit page {{mediawiki|Help:Extension:ParserFunctions|ParserFunctions}} and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the {{mediawiki|Extension:ParserFunctions|ParserFunctions extension}}. To install it, you'll need system admin access to the server of your MediaWiki installation.
  
Another dependency that may be used in templates, especially those on Wikipedia, is Lua. Having <code><nowiki>{{#invoke: }}</nowiki></code> in template code is a good sign for it. In case it's used, you need to install the {{mediawiki|Extension:Scribunto|Scribunto extension}} and system admin access is required too. See that page for more instructions about installing and using the extension.
+
<!--T:102-->
 +
Another dependency that may be used in templates, especially those on Wikipedia, is Lua. Having <tvar|invoke><code><nowiki>{{#invoke: }}</nowiki></code></> in template code is a good sign for it. In case it's used, you need to install the {{mediawiki|Extension:Scribunto|Scribunto extension}} and system admin access is required too. See that page for more instructions about installing and using the extension.
  
=== CSS and JavaScript code ===
+
=== CSS and JavaScript code === <!--T:103-->
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.
 
  
# 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.
+
<!--T:104-->
 +
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 privileges, because you'll be editing system messages in the "MediaWiki:" namespace.
 +
 
 +
<!--T:105-->
 +
# 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 those classes to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.</translate>
 +
<translate>
 +
<!--T:106-->
 
# 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.
 
# 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.
  
==See also==
+
==Note== <!--T:123-->
*[[Help:External searches]] – a template special use case example
+
 
*[[Help:Magic words]] – fancy stuff you may find in some templates
+
<!--T:124-->
*[[Help:Parser functions in templates]]
+
An experiment shows that if you include a template in itself, it won't put MediaWiki into an infinite recursion.</translate>
*{{meta|Help:Template}} – contains a much more thorough manual on how exactly templates function
+
<translate>
**{{meta|Help:Advanced templates}} – describes even more advanced techniques such dynamic template calls and variable variables
+
<!--T:125-->
*{{meta|Help:Embed page}} – embedding pages from [[Help:namespaces|namespaces]] other than <code>Template:</code>.
+
MediaWiki will find out, and stop with the template's name in bold.</translate>
 +
<translate>
 +
<!--T:126-->
 +
For example, if the content of "Template:Aaaa" is "<tvar|code><code><nowiki>{{Aaaa}}</nowiki></code></>", it'll show as "<tvar|result>'''Template:Aaaa'''</>".
 +
 
 +
== See also == <!--T:107-->
 +
</translate>
 +
 
 +
* {{ll|Help:TemplateData}}
 +
<translate>
 +
<!--T:108-->
 +
* <tvar|external>{{ll|Help:External searches}}</> – a template special use case example
 +
</translate>
 +
<translate>
 +
<!--T:109-->
 +
* <tvar|magic>{{ll|Help:Magic words}}</> – fancy stuff you may find in some templates
 +
</translate>
 +
* {{ll|Help:Parser functions in templates}}
 +
<translate>
 +
<!--T:111-->
 +
* [[<tvar|meta>m:Special:MyLanguage/Help:Template</>|m:Help:Template]] – contains a much more thorough manual on how exactly templates function
 +
</translate>
 +
<translate>
 +
<!--T:112-->
 +
** [[<tvar|meta>m:Special:MyLanguage/Help:Advanced templates</>|m:Help:Advanced templates]] – describes even more advanced techniques such as dynamic template calls and variable variables
 +
</translate>
 +
<translate>
 +
<!--T:113-->
 +
* [[<tvar|meta>m:Help:Embed page</>|m:Help:Embed page]] – embedding pages from [[<tvar|ns>Special:MyLanguage/Help:Namespaces</>|namespaces]] other than <code>Template:</code>.
 +
</translate>
 +
* {{ll|Manual:Importing Wikipedia infoboxes tutorial}}
  
[[Category:Help|{{PAGENAME}}]]
+
[[Category:Help{{translation}}|Templates]]
[[Category:Template]]
+
[[Category:Template{{translation}}]]
{{Languages|Help:Templates}}
+
[[Category:MediaWiki for site admins{{translation}}]]

2016年7月24日 (日) 00:25時点における最新版

PD Note: When you edit this page, you agree to release your contribution under the CC0. See Public Domain Help Pages for more info.
PD
</div>

If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play. Unlike extensions and media files, there is no central repository for templates. Templates can be newly written or to save duplicating work already done, exported from another wiki e.g. Wikipedia, and then imported into the target wiki.

Creation[編集]

Templates are standard wiki pages 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; besides this, you can create them like any other wiki page.

The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents:

Hello! Welcome to the wiki.

you'll have created your first template! If you then insert the code:

{{Welcome}}

in any other page, when that page is viewed the text "Hello! Welcome to the wiki." will appear instead of {{Welcome}}. The template content is "transcluded" into the other page, i.e. it is integrated in the page.

You can then insert {{Welcome}} 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:

Hi there! Welcome to this wonderful wiki.

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.

This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.

Usage[編集]

Templates can be used in other pages in these ways:

  • {{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 link will remain unchanged in the page's source.
  • {{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. Note: later changes to the source template page will not be propagated to the page with the template link.
  • {{safesubst:Name}} — this was introduced to allow for substitution that doesn't break transclusion, see w:en:Help:Substitution#The safesubst: modifier.
  • {{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.

In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, 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]]

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 talk page of other users, such as:

Example sunflower image
A little thank you...
for all your effort.
hugs, Me


The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user be 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 Template:Thankyou, 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:

'''A little thank you...'''
for {{{1}}}.
hugs, {{{2}}}

Notice the use of {{{1}}} and {{{2}}}. 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: {{{ }}}. 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 (|). 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:

{{Thankyou|all your effort|Me}}

In this case, the {{Thankyou}} template receives parameters {{{1}}}=all your effort and {{{2}}}=Me, producing:

Example sunflower image
A little thank you...
for all your effort.
hugs, Me


The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:

{{Thankyou|Me|all your effort}}

would produce this result:

Example sunflower image
A little thank you...
for Me.
hugs, all your effort


Note: identifying parameters by order (with {{{1}}}, 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.

Note: If an equal sign appears inside an anonymous template parameter, it may be interpreted as a named parameter, treating the text before the equal sign as the parameter name and the text after it as the value. This is a common problem when you need to include an external link, or an HTML element with attributes (see Task 16235) The workaround is to use named parameters instead, or even numbered parameters as explained in the following section.

Numbered parameters[編集]

To pass in parameters by number, identify each parameter when passing it:

{{Thankyou|2=Me|1=your friendship}}

This time, template {{Thankyou}} receives parameters {{{1}}}=your friendship and {{{2}}}=Me, though they have been supplied in inverse order, and produces:

Example sunflower image
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:

'''A little thank you...'''
for {{{reason}}}.
hugs, {{{signature}}}

Within the template, we use {{{reason}}} and {{{signature}}} to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it:

{{Thankyou|signature=Me|reason=being who you are}}

In this case, template {{Thankyou}} receives parameters {{{reason}}}=being who you are and {{{signature}}}=Me and produces:

Example sunflower image
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:

{{Thankyou}}

in the numbered parameters example above you would get the following:

Example sunflower image
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:

'''A little thank you...'''
for {{{reason|everything}}}.
hugs, {{{signature|Me}}}

then {{{reason|everything}}} defines that if no parameter {{{reason}}} is provided, then the value everything will be used. Similarly, {{{signature|Me}}}, defaults parameter {{{signature}}} to value Me. Now, transcluding the template again without passing any parameter, results in the following:

Example sunflower image
A little thank you...
for everything.
hugs, Me


Order of evaluation[編集]

Template parameters are fully evaluated before they are passed to the template. So if one of your parameters is a call to another template, what is actually passed is the expanded parameter.

{{Thankyou|reason=visiting ({{Welcome}})|signature=Me}}

becomes

{{Thankyou|reason=visiting (Hi there! Welcome to this wonderful wiki.)|signature=Me}}

before it becomes:

Example sunflower image
A little thank you...
for visiting (Hi there! Welcome to this wonderful wiki.).
hugs, Me


In a few circumstances, this order of evaluation makes a difference from the alternative. (The alternative being to check the results of the template call to see if there are additional templates to be expanded.)

First, the name of the template can be expanded at the same time and in the same way as the parameters.

Thus

{{Thankyou in {{preferred language}}|signature=Me}}

might expand to

{{Thankyou in french|signature=Me}}

So that a template called Thankyou in french could display the thank you in the preferred language (however that was determined.)

Secondly, the template being called can't construct a template from pieces to be called later. A template that took the form {{start of call}}{{end of call}} would not work if start of call was {{Thankyou| and end of call was signature=Me}}.

Thirdly, the parameter being passed is expanded only once, rather than every place the parameter is used in the template. This is more efficient on the mediawiki server.

Finally, the template being called can't stop the parameter from being expanded altogether. Even if a template only uses a parameter in some circumstances, it will always have been expanded before the call was made.

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 <noinclude> and <includeonly> tags.

Anything between <noinclude> and </noinclude> 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:

  • Category links when categorizing the template itself
  • interlanguage links to similar templates in other languages
  • Explanatory text about how to use the template

Likewise, anything between <includeonly> and </includeonly> 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:

  • 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.

Everything outside <noinclude> and <includeonly> 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. The focus is on what is inside these two tags.

Everything outside <onlyinclude> tags is discarded in the transclusion. Even sections tagged includeonly are discarded in the transclusion unless they are also tagged as onlyinclude. The focus is on what is outside this tag.

The three partial transclusion tags enable all possible combinations of what is processed and rendered. Comments also fill a role.

Organizing templates[編集]

For templates to be effective, users need to find them, and find out how to use them.

To find them, users can:

  1. Click 特別ページ > 全ページ
  2. In the 名前空間: list, choose Template and click 表示.

To give usage information, include an example like this one on the template page:

<noinclude>
== Usage ==
Welcome users:
{{Thankyou|reason=your reason|signature=your signature}}
</noinclude>

Then, an editor can simply copy and paste the example to use the template.

Linking to a template[編集]

To link to a template instead of including it, use the {{tl}} template. The name of the template the editor wants to link to goes after the vertical pipe. For example: {{tl|navbar}} links to the {{navbar}} template.

Copying from one wiki to another[編集]

Templates often require CSS or other templates, so users frequently have trouble copying templates from one wiki to another. The steps below should work for most templates.

MediaWiki code[編集]

If you have import rights (Specifically importupload) on the new wiki:

  1. 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 "テンプレートを含める".
    • Uncheck the box "完全な履歴は含めず、最新版のみを含める".
    • Click "書き出し".
  2. Go to Special:Import on the new wiki and upload the .xml file.

If you don't have import rights on the new wiki:

  1. Go to the template you want to copy from the original wiki. Go to the edit page, and copy all the wikitext
  2. On the new wiki, go to the page with the same name as the template you copied. Hit create/edit and paste the wikitext you copied. In the edit summary of each template, link to the original page for attribution.
  3. Back in the original wiki at the edit window, below the edit box, look at the list of "Templates used on this page". For each template listed follow these instructions. Also do that for any template used by any of these templates, and so on.

This will copy the entire code necessary, and will suffice for some templates. If it doesn't work also check for red links listed under "Pages transcluded onto the current version of this page:", below the edit box. If there are any repeat the above steps for these as well.

After successfully importing the template and all its linked templates from the other wiki, edit it to change customisations to suit your wiki. For example to change a logo, remove redundant categories or red links.

Extensions[編集]

An extension often used in templates is ParserFunctions. Visit page ParserFunctions and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the ParserFunctions extension. To install it, you'll need system admin access to the server of your MediaWiki installation.

Another dependency that may be used in templates, especially those on Wikipedia, is Lua. Having {{#invoke: }} in template code is a good sign for it. In case it's used, you need to install the Scribunto extension and system admin access is required too. See that page for more instructions about installing and using the extension.

CSS and JavaScript 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 privileges, because you'll be editing system messages in the "MediaWiki:" namespace.

  1. Look for the use of CSS classes (text like class="foobar") in the template text. If those classes appear in "MediaWiki:Common.css" or "MediaWiki:Monobook.css" on the original wiki, copy those classes to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.
  2. 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.

Note[編集]

An experiment shows that if you include a template in itself, it won't put MediaWiki into an infinite recursion. MediaWiki will find out, and stop with the template's name in bold. For example, if the content of "Template:Aaaa" is "{{Aaaa}}", it'll show as "Template:Aaaa".

See also[編集]

案内メニュー