Help:Templates

From FamiLAB Wiki
Revision as of 05:35, 6 February 2011 by 24.205.36.93 (talk) (Parameters)
Jump to: navigation, search

Template:PD Help Page 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; 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}}, described above, '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.
  • {{safesubst:Name}} was introduced in rev:61710 to allow for substitution that doesn't break transclusion, see w:en:Help:Substitution#safesubst:.
  • {{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]]

aaaaa

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 {{ #ifeq: FamiLAB Wiki | MediaWiki | ParserFunctions extension

| [[{{#if:|{{#ifexist:Extension:ParserFunctions||mediawikiwiki:}}}}Extension:ParserFunctions|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 {{ #ifeq: FamiLAB Wiki | MediaWiki | ParserFunctions extension

| [[{{#if:|{{#ifexist:Extension:ParserFunctions||mediawikiwiki:}}}}Extension:ParserFunctions|ParserFunctions extension]] }}.

See also