Difference between revisions of "Help:Templates"

From eLinux.org
Jump to: navigation, search
m (1 revision)
(Usage: add example of template substitution from main namespace, {{subst::Pagename}}. see [http://www.mediawiki.org/wiki/Help_talk:Templates#Unclear_behavior_of_template_substitution note here])
Line 9: Line 9:
  
 
*<code><nowiki>{{Name}}</nowiki></code> 'transcludes' (i.e. includes a copy of) the content of the template (stored in the page <nowiki>[[Template:Name]]</nowiki>) whenever the page containing the template transclusion is fetched and displayed; i.e. if the template is later changed, the displayed transcluding page will automatically change too
 
*<code><nowiki>{{Name}}</nowiki></code> 'transcludes' (i.e. includes a copy of) the content of the template (stored in the page <nowiki>[[Template:Name]]</nowiki>) whenever the page containing the template transclusion is fetched and displayed; i.e. if the template is later changed, the displayed transcluding page will automatically change too
*<code><nowiki>{{subst:Name}}</nowiki></code> replaces that string with the contents of the template, in the source of the transcluding page, when you save that page; the copy of the template contents can then be edited normally (and separately from the original in the template page). '''''Note''''': don't use this if you are looking to continually propagate changes from the source template to the page(s) that references it.
+
*<code><nowiki>{{subst:Name}}</nowiki></code> replaces that string with the contents of the template, in the source of the transcluding page, when you save that page; the copy of the template contents can then be edited normally (and separately from the original in the template page). To '''''Note''''': don't use this if you are looking to continually propagate changes from the source template to the page(s) that references it.
 
*<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
 
*<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
  
Line 17: Line 17:
 
*<code><nowiki>{{Foo:Pagename}}</nowiki></code>      includes <code><nowiki>[[Foo: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>{{: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:
Line 100: Line 101:
 
==Copying from one wiki to another==
 
==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:
 
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:
# Copy the template text to the new wiki, either by [http://meta.wikimedia.org/wiki/Help:Import importing] or with a link in the edit summary to the original page (for attribution).
+
 
 +
=== If you have import rights on the new wiki ===
 +
# Go to [[Special:Export]] on the original wiki. Enter the name of the template in the big text box, check "Include templates" and click Export. This will download a .xml file.
 +
# Go to [[Special:Import]] on the new wiki and upload the .xml file.
 +
# Look for CSS classes (like class="foobar") 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.
 +
# If the template uses [[Help:Extension:ParserFunctions|ParserFunctions]], you have to install the {{mediawiki|Extension:ParserFunctions|ParserFunctions extension}}.
 +
 
 +
=== If you don't have import rights on the new wiki ===
 +
# Copy the template text to the new wiki; link to the original page in the edit summary for attribution.
 +
# Edit the template on the new wiki, and look through the list of templates at the bottom. The ones in red will also need to be copied from the original wiki to the new wiki. You may have to repeat this process multiple times before all dependency templates have been recognized and copied.
 
# Look for CSS classes (like class="foobar") 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.
 
# Look for CSS classes (like class="foobar") 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.
# Look for templates (like <nowiki>{{foobar}}</nowiki>) inside the template text, and copy those templates to the new wiki.
 
 
# If the template uses [[Help:Extension:ParserFunctions|ParserFunctions]], you must install the {{mediawiki|Extension:ParserFunctions|ParserFunctions extension}}.
 
# If the template uses [[Help:Extension:ParserFunctions|ParserFunctions]], you must install the {{mediawiki|Extension:ParserFunctions|ParserFunctions extension}}.
  

Revision as of 22:18, 29 December 2009

PD Important note: When you edit this page, you agree to release your contribution into the public domain. If you don't want this or can't do this because of license restrictions, please don't edit. This page is one of the Public Domain Help Pages, which can be freely copied into fresh wiki installations and/or distributed with MediaWiki software; see Help:Contents for an overview of all pages. See Project:PD help/Copying for instructions. PD

If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play.

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. Therefore you can create them like any other wiki page.

Usage

Templates are wiki pages which can be used in other pages in three ways:

  • {{Name}} 'transcludes' (i.e. includes a copy of) the content of the template (stored in the page [[Template:Name]]) whenever the page containing the template transclusion is fetched and displayed; i.e. if the template is later changed, the displayed transcluding page will automatically change too
  • {{subst:Name}} replaces that string with the contents of the template, in the source of the transcluding page, when you save that page; the copy of the template contents can then be edited normally (and separately from the original in the template page). To Note: don't use this if you are looking to continually propagate changes from the source template to the page(s) that references it.
  • {{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

Template with numbered parameters
 
'''A little thank you...'''<br />
<small>for {{{1}}}.<br />
hugs, {{{2}}}</small>
You type You get
{{Thankyou|all your hard work|Joe}}
Example sunflower image
A little thank you...
for all your hard work.
hugs, Joe
with named parameters
 
'''A little thank you...'''<br />
<small>for {{{reason}}}.<br />
hugs, {{{signature}}}</small>
You type You get
{{Thankyou
|reason=all your hard work
|signature=Joe}}
Example sunflower image
A little thank you...
for all your hard work.
hugs, Joe

You can define parameters in templates either numbered as {{{1}}} or named {{{param}}}. Please note that there are a set of three curly braces ({{{ and }}}) around each parameter. This is different than normal template name usage.

Example: You want a little thank you note you can put on the talk page of other users. It will contain a reason and your signature. You could create Template:Thankyou to enter your text, as in the example in the table.

When using the template on a page, you fill in the parameter values, separated by a pipe char (|): {{Thankyou|all your hard work|Joe}}. For named parameters use "name=value" pairs separated by a pipe char: {{Thankyou|reason=all your hard work|signature=Joe}}. The advantage of using named parameters in your template is that they are flexible in order. It also makes the template easier to understand if you have many parameters. If you want to change the order of numbered parameters, you have to mention them explicitly: {{Thankyou|2=Joe|1=all your hard work}}.

You can also provide default values for parameters, i.e. values that are going to be used if no value is provided for a parameter. For example, {{{reason|all your hard work}}} would result in "all your hard work" if no value was provided for the parameter reason.

Control template inclusion

You can control template inclusion by the use of <noinclude> and <includeonly> tags.

Anything between <noinclude> and </noinclude> will be processed and displayed only when the template's page is being viewed directly.

Possible applications are:

  • Categorising templates
  • Interlanguage links to similar templates in other languages
  • Explanatory text about how to use the template

The converse is <includeonly>. Text between <includeonly> and </includeonly> will be processed and displayed only when the page is being included. The obvious application is to add all pages containing a given template to a category, without putting the template itself into that category.

Note: when you change the categories applied by a template, the categorization of the pages that use that template may not be updated until some time later: this is handled by the job queue.

Organizing templates

For templates to be effective, users need to find them and be able to use them. A simple technique is to include an example on the template page. For example:

<noinclude>
== Usage ==
Allows to establish a link to a subject:
{{NameOfTemplate|Term1+Term2+Term3}}
</noinclude>

Then, an editor can simply copy and paste the example to create a similar page.

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:

If you have import rights on the new wiki

  1. Go to Special:Export on the original wiki. Enter the name of the template in the big text box, check "Include templates" and click Export. This will download a .xml file.
  2. Go to Special:Import on the new wiki and upload the .xml file.
  3. Look for CSS classes (like class="foobar") 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.
  4. If the template uses ParserFunctions, you have to install the ParserFunctions extension.

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

  1. Copy the template text to the new wiki; link to the original page in the edit summary for attribution.
  2. Edit the template on the new wiki, and look through the list of templates at the bottom. The ones in red will also need to be copied from the original wiki to the new wiki. You may have to repeat this process multiple times before all dependency templates have been recognized and copied.
  3. Look for CSS classes (like class="foobar") 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.
  4. If the template uses ParserFunctions, you must install the ParserFunctions extension.

See also

Language: English