Help:Templates: Difference between revisions

From 118Wiki
Jump to navigation Jump to search
(Link to Mediawiki: Help:Templates)
(removed safesubst (got it a bit wrong + less relevant to the topic))
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Templates are a way to include commonly reused segments of text or code. They also make pages sources easier to read by removing complicated elements.
{{118Wikiheader}}
{{Help page}}


==Example==
'''Templates''' are bits of code that make using the wiki easier. They are used in nearly every page in 118Wiki, including the very basic character pages. This guide explains what the templates are and how to use them on a page.


For instance adding <nowiki>{{Template:NPC ship}}</nowiki> to a page makes it add this:
{{LCARS-bar-heading|What is a template?|COLOR={{Color|red}}}}
{{Template:NPC ship}}
A template is a piece of code that can be used on multiple pages. A very simple template could be just a string of text, but most of them contain an amount of coding and programming using HTML, CSS, magic words, parser functions and other methods.


This is a reasonably large segment of code, that would be inconvenient to add to many pages that require it, so it as added as a template in order to make it easier.
Using a template has several advantages, as it cuts down the required effort of having to write or copy the code to each page individually. This is especially important when the code is complicated. If the code needs to be modified, the changes can be done to the template instead of having to look up each page that uses similar code. Additionally, templates help to achieve similar visuals for each page.


==Guidelines==
{{LCARS-bar-heading|Using templates|COLOR={{Color|gold}}}}


Before we talk about how to make Templates, it is a good idea to consider '''when''' we should use templates.
=== Template call ===
The contents of a template can be added (or ''transcluded'') to a page using a ''template call''. A template call is an instruction for the system to search for that template and show its content on the ''target page'' when someone is reading it. As such, the template calls are similar to function calls in programming languages.


# Templates should only be used for code which is likely to be re-used on a number of pages.
<div style="padding: 5px 10px 5px; border-left: 5px solid lightpink;">
# Templates are unnecessary for short segments.
In source editing, a template can be called using two curly brackets around the name of the template without the “Template:” prefix. For example, <code><nowiki>{{Mirror universe}}</nowiki></code> calls the [[Template:Mirror universe]]. It is a template used to inform the readers that the page covers a [[Mirror universe]] topic. Below is what it would look like:


For instance using a template to include a universal navigation for a set of pages in a series is a good use, while creating a template for the "Stats" segment on your characters page is unnecessary.
{{Mirror universe}}</div>


==How to create a template==
<div style="padding: 5px 10px 5px; border-left: 5px solid lightgreen;">In the Visual Editor, you can add a template call to a page by opening the “Insert” menu from the toolbar and clicking “Template”. This will open a dialog box (popup) with a search bar for finding the template. After selecting a template, you may add the template to the page by clicking “Insert” in the top right corner of the dialog box.</div>


A template can be created in the same way as a ordinary page, just instead of just typing the pages name you type Template: before it to mark it as a template.
=== Parameters ===


==More help on templates==
Templates can also take information the user gives them and change its appearance or functionality depending on your input. For this purpose, you would use ''parameters''. When calling a template, you have an option to give a value to a parameter. The value given for a parameter can be text, files, code or even other templates.
* [http://www.mediawiki.org/wiki/Help:Templates Mediawiki: Help:Templates]
 
<div style="padding: 5px 10px 5px; border-left: 5px solid lightgreen;">
In Visual Editor, the dialogue will ask values to each parameter separately. You can choose which parameters you want to use.</div>
 
<div style="padding: 5px 10px 5px; border-left: 5px solid lightpink;">
In source editing, parameters are separated in the template call by | symbol (vertical bar). Parameters and what they do can typically be found on the template documentation page. There are two types of parameters, named and unnamed.
 
If the parameter is unnamed, the value given to it is separated from the templates name and other parameters by a vertical bar.
 
For example, [[Template:Crewnav]] can be used on a character page to show the officers who serve on the same ship as that character. Since Template:Crewnav uses a single unnamed parameter, you would give the value with a vertical bar separating it from the template name: <code><nowiki>{{Crewnav|USS Artemis-A}}</nowiki></code>. The result is below:
{{Crewnav|USS Artemis-A}}
 
A template may sometimes have multiple unnamed parameters. They are separated from each other using the vertical bar. In that case, the parameters always have to be in the same order!
 
For example, if you want to show one of the [[ribbons]] your character has earned, you can use [[Template:Ribbons]]. The template requires that you give the name of the ribbon, how large you want the ribbon to be in pixels and how many times the character has earned the ribbon, in this order. So, you could have a template call like <code><nowiki>{{Ribbons|Innovation Ribbon|120|2}}</nowiki></code>, with the result:
{{Ribbons|Innovation Ribbon|120|2}}
 
{{Quote box | quote = Tea, Earl Grey, hot | source = —Jean-Luc Picard | title = Example }}
Instead of unnamed parameters, many templates have named parameters. They are also separated from the template name and each other with vertical bars. The parameter’s name is given, followed by the equality sign =, and lastly the value given to the parameter.
For example, if you would want to show a quote someone said, you may use a [[Template:Quote box]] with a template call like <code><nowiki>{{Quote box | quote = Tea, Earl Grey, hot | source = —Jean-Luc Picard | title = Example }}</nowiki></code>, with the result on the right.
 
Sometimes you might want to use unnamed parameters out of order. In that case, the ordinal of the unnamed parameter can be used as the parameter name, i.e. the first parameter has the name “1”. As such, the example with the ribbons also works with the code <code><nowiki>{{Ribbons|2=120|1=Innovation Ribbon|3=2}}</nowiki></code>.</div>
 
{{LCARS-bar-heading|Learn more|COLOR={{Color|blue}}}}
=== Transclusion and substitution ===
As described above, adding a template to a page using a template call (such as <code><nowiki>{{Mirror universe}}</nowiki></code>) is called ''transclusion''. Each time the page is opened, the current form of the template is displayed.
 
Sometimes, it is more desirable that the display doesn’t change but is static or that it may be modified for the purpose of that one page. The code may be copy-pasted, but this might break it. For example, the inclusion tags should be removed by the user by hand. Instead of copy-pasting, ''substitution'' should be used. When the page is saved, substitution replaces the template call with the template code as it would appear had the template merely been transcluded.
 
For example, if [[Template:Mirror universe]] is substituted to a page, the template call would be replaced by the following code:<br>
<nowiki>:''This article pertains to a subject of the [[Mirror universe]].''[[File:Mirror-universe.jpg|x40px|link=Category:Mirror universe]]<br clear=all></nowiki>
Now, should the template be edited, the display on the page won’t change. Note that the code of the template in its template page has a line <code><nowiki><noinclude>[[Category:Mirror universe]]</noinclude></nowiki></code>. Since this line is not included in the transclusion, it won’t be in the substitution either.
 
<div style="padding: 5px 10px 5px; border-left: 5px solid lightpink;">In source editing, substitution is executed with word <code>subst:</code> in the template call, e.g. <code><nowiki>{{subst:Mirror universe}}</nowiki></code></div>
 
Note that substitution will increase the length of the wikicode on page considerably and might make difficult to edit. Some templates are designed so that they are dynamic, so substitution may defeat the purpose.
 
The wikicode of a template may also be called.
<div style="padding: 5px 10px 5px; border-left: 5px solid lightpink;">In source editing, wikicode call is executed with word <code>msgnw:</code> in the template call, e.g. <code><nowiki>{{msgnw:Mirror universe}}</nowiki></code></div>
 
=== Other pages ===
This help page has concentrated on templates, but everything stated here about templates applies to any other page as well! For example, the contents of an article may be transcluded or substituted to another page. For example, many crew listings depend on character ID pages, such as [[Tristan Wolf/ID]], which doesn’t have the “Template:” prefix and isn’t therefore a template.
 
<div style="padding: 5px 10px 5px; border-left: 5px solid lightpink;">When transcluding or substituting a page other than a template, the template call is slightly different in source editing. The prefix “Template:” is left out, but the ''namespace'' prefix should be used for any other type of page. For example, this help page is called with <code><nowiki>{{Help:Templates}}</nowiki></code>. For main namespace (articles), the namespace prefix is empty, so only a colon : is used, for example <code><nowiki>{{:Tristan Wolf}}</nowiki></code> for [[Tristan Wolf]].
 
Substitution of another page works similarly, but with <code><nowiki>subst:</nowiki></code> (or equivalent) ahead of the namespace prefix, e.g. <code><nowiki>{{subst:Help:Templates}}</nowiki></code> or <code><nowiki>{{subst::Tristan Wolf}}</nowiki></code></div>
 
=== More on the topic ===
{{Start Columns}}
* [[:Category:Templates]], category that contains most templates in 118Wiki
* [[Special:MostTranscludedPages]], an automatically updated page listing the most commonly used templates (and some other pages)
{{Column|50%}}
* [https://www.mediawiki.org/wiki/Help:Templates Templates], MediaWiki help page about templates
* [https://en.wikipedia.org/wiki/Help:Template Template], Wikipedia help page about templates
* [https://www.mediawiki.org/wiki/Help:Substitution Substitution], MediaWiki help page about substitution
{{End Columns}}


[[Category:Help]]
[[Category:Help]]

Latest revision as of 11:12, 5 October 2024

118Wiki


Templates are bits of code that make using the wiki easier. They are used in nearly every page in 118Wiki, including the very basic character pages. This guide explains what the templates are and how to use them on a page.

What is a template?

A template is a piece of code that can be used on multiple pages. A very simple template could be just a string of text, but most of them contain an amount of coding and programming using HTML, CSS, magic words, parser functions and other methods.

Using a template has several advantages, as it cuts down the required effort of having to write or copy the code to each page individually. This is especially important when the code is complicated. If the code needs to be modified, the changes can be done to the template instead of having to look up each page that uses similar code. Additionally, templates help to achieve similar visuals for each page.

Using templates

Template call

The contents of a template can be added (or transcluded) to a page using a template call. A template call is an instruction for the system to search for that template and show its content on the target page when someone is reading it. As such, the template calls are similar to function calls in programming languages.

In source editing, a template can be called using two curly brackets around the name of the template without the “Template:” prefix. For example, {{Mirror universe}} calls the Template:Mirror universe. It is a template used to inform the readers that the page covers a Mirror universe topic. Below is what it would look like:

This article pertains to a subject of the Mirror universe.Mirror-universe.jpg
In the Visual Editor, you can add a template call to a page by opening the “Insert” menu from the toolbar and clicking “Template”. This will open a dialog box (popup) with a search bar for finding the template. After selecting a template, you may add the template to the page by clicking “Insert” in the top right corner of the dialog box.

Parameters

Templates can also take information the user gives them and change its appearance or functionality depending on your input. For this purpose, you would use parameters. When calling a template, you have an option to give a value to a parameter. The value given for a parameter can be text, files, code or even other templates.

In Visual Editor, the dialogue will ask values to each parameter separately. You can choose which parameters you want to use.

In source editing, parameters are separated in the template call by | symbol (vertical bar). Parameters and what they do can typically be found on the template documentation page. There are two types of parameters, named and unnamed.

If the parameter is unnamed, the value given to it is separated from the templates name and other parameters by a vertical bar.

For example, Template:Crewnav can be used on a character page to show the officers who serve on the same ship as that character. Since Template:Crewnav uses a single unnamed parameter, you would give the value with a vertical bar separating it from the template name: {{Crewnav|USS Artemis-A}}. The result is below:

NPC Listing   ·   USS Artemis-A Crew Manifest   ·   Crew History
2400-MacKenzie-WM-Long.png
Commanding Ofc.
Addison MacKenzie
JK - Red Picard v5.jpg
Executive Ofc.
Talos Dakora
Sil-Picard Uniform.png
Chief Tactical Ofc.
Vitor Silveira
Salkath1.png
Engineering Ofc.
Salkath
Imril-Avatar-LD-Style-lores2.png
Engineering Ofc.
Imril
Jovenan Lt 2401.png
Chief Science Ofc.
Jovenan
Lyara.png.a74aa194814329054780237adc48ffdd.png
Helm/Ops Ofc.
Lyara Alroyo
Ollie Bergman Ensign.png
Operations Ofc.
Ollie Bergmen
Ensign Savel.png
Security Ofc.
Savel
Roster-blank.jpg
Security Ofc.
Kel Solas
Gnai.png
Science Ofc.
Gnai
Genkos Adea.png
Chief Medical Ofc.
Genkos Adea
Edit This Nav

A template may sometimes have multiple unnamed parameters. They are separated from each other using the vertical bar. In that case, the parameters always have to be in the same order!

For example, if you want to show one of the ribbons your character has earned, you can use Template:Ribbons. The template requires that you give the name of the ribbon, how large you want the ribbon to be in pixels and how many times the character has earned the ribbon, in this order. So, you could have a template call like {{Ribbons|Innovation Ribbon|120|2}}, with the result:

Award-goldstar.png
Awards ServiceRibbons Innovationribbon 2014.jpg


Example

Tea, Earl Grey, hot

—Jean-Luc Picard

Instead of unnamed parameters, many templates have named parameters. They are also separated from the template name and each other with vertical bars. The parameter’s name is given, followed by the equality sign =, and lastly the value given to the parameter. For example, if you would want to show a quote someone said, you may use a Template:Quote box with a template call like {{Quote box | quote = Tea, Earl Grey, hot | source = —Jean-Luc Picard | title = Example }}, with the result on the right.

Sometimes you might want to use unnamed parameters out of order. In that case, the ordinal of the unnamed parameter can be used as the parameter name, i.e. the first parameter has the name “1”. As such, the example with the ribbons also works with the code {{Ribbons|2=120|1=Innovation Ribbon|3=2}}.

Learn more

Transclusion and substitution

As described above, adding a template to a page using a template call (such as {{Mirror universe}}) is called transclusion. Each time the page is opened, the current form of the template is displayed.

Sometimes, it is more desirable that the display doesn’t change but is static or that it may be modified for the purpose of that one page. The code may be copy-pasted, but this might break it. For example, the inclusion tags should be removed by the user by hand. Instead of copy-pasting, substitution should be used. When the page is saved, substitution replaces the template call with the template code as it would appear had the template merely been transcluded.

For example, if Template:Mirror universe is substituted to a page, the template call would be replaced by the following code:

:''This article pertains to a subject of the [[Mirror universe]].''[[File:Mirror-universe.jpg|x40px|link=Category:Mirror universe]]<br clear=all>

Now, should the template be edited, the display on the page won’t change. Note that the code of the template in its template page has a line <noinclude>[[Category:Mirror universe]]</noinclude>. Since this line is not included in the transclusion, it won’t be in the substitution either.

In source editing, substitution is executed with word subst: in the template call, e.g. {{subst:Mirror universe}}

Note that substitution will increase the length of the wikicode on page considerably and might make difficult to edit. Some templates are designed so that they are dynamic, so substitution may defeat the purpose.

The wikicode of a template may also be called.

In source editing, wikicode call is executed with word msgnw: in the template call, e.g. {{msgnw:Mirror universe}}

Other pages

This help page has concentrated on templates, but everything stated here about templates applies to any other page as well! For example, the contents of an article may be transcluded or substituted to another page. For example, many crew listings depend on character ID pages, such as Tristan Wolf/ID, which doesn’t have the “Template:” prefix and isn’t therefore a template.

When transcluding or substituting a page other than a template, the template call is slightly different in source editing. The prefix “Template:” is left out, but the namespace prefix should be used for any other type of page. For example, this help page is called with {{Help:Templates}}. For main namespace (articles), the namespace prefix is empty, so only a colon : is used, for example {{:Tristan Wolf}} for Tristan Wolf. Substitution of another page works similarly, but with subst: (or equivalent) ahead of the namespace prefix, e.g. {{subst:Help:Templates}} or {{subst::Tristan Wolf}}

More on the topic

  • Templates, MediaWiki help page about templates
  • Template, Wikipedia help page about templates
  • Substitution, MediaWiki help page about substitution