Templates

From SubSurfWiki
Jump to navigation Jump to search

Templates let you transclude (include, essentially) wikitext from one page onto another page. You can transclude from any page to any other, but the wiki is designed to let you most easily transclude from the Template namespace.

Any time you find yourself repeating a piece of wikitext from one page to another, you need a template. Templates are also a nice way to hide ugly or intimidating code (HTML, CSS, and wikitext) from users.

Being able to transclude content is one thing, and useful in itself, but the real power comes when you change their behaviour by passing parameters to your template, or when you make them adapt to their environment like the namespace they are being used in.

Templates turn the wiki from a document repository into a fully programmable development environment.

Make a simple template

  • Go to Template:Test/Yourname and add some wikitext — some text, a table, an image, or anything you like.
  • Go to Special:MyPage and add this: {{Test/Yourname}} (you don't need to specify the namespace Template, it is assumed.
  • Voila!

Make a more interesting template

  • Make it do something more interesting — put it in a box, perhaps (for example, use <div style="width:20%; border:1px; float:right;"> or use {{ambox}})
  • Think about a group of pages you work on and make some small piece of branding for them, or perhaps make a {{navbox}}

Pass a parameter

  • Change some behaviour of the template by passing a parameter, e.g. some text or a setting
    • In the template's wikitext, use and unnamed variable {{{1|}}} or a named one like {{{myparameter|}}} (the pipe says "if this is not here, do nothing")
    • In the page you are calling the template from, add the variable to the template call like this: {{Test/Yourname|awesome!}} or {{Youname|myparameter=awesome!}}

Make the template adapt

  • Make the template show a different thing when the page it's on is in the User namespace, or when the page name starts with Seismic
  • To do this, you might need to use Parser Functions or Magic Words. There's good help for those on mediawiki.org.

Add documentation

  • Add documentation for the template by adding this to the template, at the end:
 <noinclude>{{Documentation}}</noinclude> 
noinclude says "don't transclude this", so it only appears on the template's own page.
  • {{Documentation}} tells the wiki to look at pagename/doc for the documentation, so we have to write it there, or just hit Create
  • Start with {{Documentation subpage}}
  • It's usually best to say what it does, give an example, explain any parameters, and add a list of related or interdependent templates
  • End with <noinclude>[[Category:An appropriate template category]]</noinclude>