On Friday, April 19, 2024 at 10:00 PM New York time, all OpenWiki Project sites will be undergoing scheduled maintenance for about 2 hours. Expect read-only access and brief periods of downtime.

Help:Creating templates

From WiKirby, your independent source of Kirby knowledge.
Revision as of 17:57, 8 November 2019 by Samwell (talk | contribs)
Jump to navigationJump to search

So by this point, an aspiring editor will have likely made use of several templates in order to improve pages or to place improvement notices on pages. The editor in question may even have made use of the option of filling in sections for some templates, such as adding a piped comment or specifying information inside an infobox. At some point, however, one may become interested in going a little further. The following is a handy guide on how to create a new template, along with some options for customization.

The very basics

A template at the very most basic is a page whose contents are copied and pasted into whichever article calls for them using its name. Templates are called using curly brackets in the same manner as linking to another page with square brackets. When a template is called, it essentially places all the information from that template page into the spot it is called to, with the exception of items listed under "<noinclude>" (more on that below). As such, the most basic template would be those such as language settings templates or short-hands which have no additional code present on them. A good example of that is the template KEEY which simply implants the line

''[[Kirby's Extra Epic Yarn]]''

wherever the template is called. This is a handy form of shorthand which reduces the amount of text that needs to be typed out, but it cannot be used in all circumstances, as it also automatically creates an internal link, which may become redundant if used several times in succession.

Defining a new template

When creating a new template, the page title must begin with Template:, or it will not register as a template. When calling that template, as seen above, the "Template:" part is not typed in, but it is typed in to any direct square bracket link.

When creating a template, it should generally consist of three main components as follows:

  1. the main template which will be transcluded into the page when called.
  2. the "includeonly" section which specifies what categories are applied to the pages which call the template.
  3. the "noinclude" section which specifies what categories the template itself appears in, as well as the documentation.

Main template

Here goes the piece that should appear in the page where it is inserted. This can be a simple piece of text, a small notice box, a more elaborate table, or some other widget like an infobox or piece of formatting code. Templates may in turn contain calls to other templates, which can result in very complicated pieces of wiki magic being contained in simple text calls.

Template example

This page will not attempt to explain how to construct the most advanced templates on the wiki, but will offer a bit of a starting point. For more basic templates with an optional pipe explanation, the editor will need to include alterable parameters into the template code. For example, here is the raw code for the About template:

:<span style="font-style:italic">This {{#if:{{{section|}}}|section|article}} is about {{{1}}}. For {{{2}}}, see {{{3}}}. {{#if:{{{4|}}}|For {{{4}}}, see {{{5}}}.}} {{#if:{{{6|}}}|For {{{6}}}, see {{{7}}}.}}  {{#if:{{{8|}}}|For {{{8}}}, see {{{9}}}.}}</span>

As can be seen, this code snippet is filled with numbers surrounded by brackets. Those are options which can be replaced with statements separated by piped lines when the template is called. In this particular case, the first three are "required", which means that if the template is inserted without any additional parameters, it will result in the template looking like this:

This article is about {{{1}}}. For {{{2}}}, see {{{3}}}.

In order to fill in those parameters when calling the template, the editor must use pipes to substitute other pieces of text into those blank spaces in the appropriate order. This can be done like so:

{{About|templates|maximum pink|[[Kirby]]}}

Which results in this:

This article is about templates. For maximum pink, see Kirby.

In addition, the template supports a number of additional optional parameters. These will only appear in the template if specifically called for. One of the more notable ones is the one that appears before the numbered parameters, in the code snippet {{#if:{{{section|}}}|section|article}}. This is a special type of parameter which requires the word in question as a parameter followed by an equals sign (=) and an additional parameter. Using this in the template call:

{{About|section=blebitty bloo|templates|maximum pink|[[Kirby]]}}

will result in the template changing its wording like so:

This section is about templates. For maximum pink, see Kirby.

As can be seen, it does not matter what is placed into the section call segment. All that matters is that something was placed in there, and the deed was done. As long as that something consists of at least one character that is not a space, it will call.

The same rule applies to the additional number calls. However, in order to use those, they must be called just like the section code was, like so:

{{About|section=blebitty bloo|templates|maximum pink|[[Kirby]]|4=our perfect ruler|5=[[King Dedede]]}}

which results in the following:

This section is about templates. For maximum pink, see Kirby. For our perfect ruler, see King Dedede.

In this case, it may be easier to simply type out the additional sentences in full after the third pipe item. Templates are meant to make life simpler, after all.

Example parameters

The above example makes use of a few different pieces to work correctly. Some of these were touched upon in the text above, but here is a more concise explanation of some of the pieces:

  • <span style="font-style:italic"></span> - This snippet of code (often referred to as "span") is used to convey a single style or other piece of formatting onto all of the material contained within. In this particular case, it makes all of the text inside italic.
  • {{#if:{{{section|}}}|section|article}} - This is an example of an if/then/else statement. The template is looking for the user to type in a piped piece titled "section=" with some sort of parameter inside. The word "section" will then appear. Otherwise, the word "article" will appear.
  • {{{1}}} - When inserted on its own, a number with triple brackets implies a fillable space which can be defined by the user with a simple pipe separation. The number determines which order the statement comes in, and will appear as is if nothing is typed in.
  • {{#if:{{{4|}}}|For {{{4}}}, see {{{5}}}.}} - This is another if/then/else statement with some differences. If the "4=" is included by the user with some kind of parameter, it will then expect a certain set of phrases to be added into that parameter. Since there is no specification for what will happen in the "else" case, nothing will appear if the "if" statement is not satisfied.

If this is still too basic for the editor's tastes, it is recommended that he/she check out the Templates help page on MediaWiki and follow that rabbit hole for more information.

"Includeonly" section

Once the main template is defined, it may be necessary to follow up with an "includeonly" section. As implied by the title, this section requires the use of the container <includeonly>. Within the container, the editor should place links to any categories that he/she wishes the template to automatically confer upon any article the template is placed into. This is handy for things like notice templates and license templates. For example, the Stub template automatically places any page it is applied to into the Stubs category.

Naturally, it is necessary to place these category links into the "includeonly" section in order to avoid placing the template itself inside that same category. For some templates, such as the About template demonstrated above, it is not necessary to include this section at all.

"Noinclude" section

This section can be seen as sort of the opposite of the "includeonly" section, in that it deals with additional text and code which applies only to the template page, and will not appear on articles where the template is called. Typically, each template will have a "noinclude" section in order to categorize it and include documentation. This section requires the use of the container <noinclude> to function.

Template documentation

Main article: Help:Template documentation

Within the "noinclude" section, a link to template documentation should be included. This is a template in and of itself, and is called by using the code {{Doc}}. From there, a sub-page should be built for the template with the suffix "/doc" attached. In the documentation page, all prescient information about how to use the template should be placed. It is handy to include a stencil of text to copy and paste into articles when calling the template, as is shown for most navbox documentation.

See also

KSA Parasol Waddle Dee Pause Screen Artwork.png