Help:Templates: Difference between revisions

From StrategyWiki, the video game walkthrough and strategy guide wiki
4,641 bytes added ,  5 March 2023
m
& deprecated
(→‎Advanced template usage: added (empty) {{#switch:}} section)
m (& deprecated)
 
(19 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Header Nav|game=StrategyWiki:Guide}}
{{Header Nav|game=StrategyWiki:Guide}}
:''Template redirects here, for a list of templates see [[:Category:Templates]]''
{{floatingtoc}}
:''Template redirects here. For a list of templates see [[:Category:Templates]]''
If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play (like the tag above which is included in the pages of the Help namespace).
If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play (like the tag above which is included in the pages of the Help namespace).


Line 10: Line 11:
Every template must be placed within a category, either [[:Category:Templates]] or [[:Category:Guide-specific templates]].
Every template must be placed within a category, either [[:Category:Templates]] or [[:Category:Guide-specific templates]].


If you want to modify a template it's usually good to discuss it within the talk page if it's a major change. Making multiple changes to templates used on multiple pages can cause disastrous effects and bog down the job queue, so it's best to discuss changes on the talk page or try your idea in a sandbox before any changes are made. Most widely-used templates, like [[Template:Infobox]] are protected to avoid this, so a discussion is the perfect way to get your ideas heard. Protected templates will require a [[StrategyWiki:Administrators|sysop/admin]] to make the change.
If you want to modify a template it's usually good to discuss it within the talk page if it's a major change. Making multiple changes to templates used on multiple pages can cause disastrous effects and bog down the job queue (increasing the server workload), so it's best to discuss changes on the talk page or try your idea in a sandbox before any changes are made. Most widely-used templates, like {{t|Game}} are protected to avoid this, so a discussion is the perfect way to get your ideas heard. Protected templates will require a [[StrategyWiki:Administrators|sysop/admin]] to make the change.


Categorization, careful naming, and maintaining a hierarchy is key in ensuring that our templates don't get cluttered.  Templates are critical for reducing redundancy, but they are powerful tools that must be given respect and much thought so that they don't in themselves become redundant.
Categorization, careful naming, and maintaining a hierarchy is key in ensuring that our templates don't get cluttered.  Templates are critical for reducing redundancy, but they are powerful tools that must be given respect and much thought so that they don't in themselves become redundant.
=== Documentation Sub-pages ===
When a template becomes widely used, such as {{t|Game}}, it's necessary to move the documentation (a.k.a. explanation) of the template to another page.
Rules for creating a documentation subpage:
*First, start <nowiki><noinclude></nowiki> tags immediately after the template code ends. This is followed by the {{t|Documentation}} template, which automatically transcludes the documentation page and a link to it. After adding the <nowiki>[[Category:Templates|{{subst:PAGENAME}}]] or [[Category:Guide-specific templates|{{subst:PAGENAME}}]]</nowiki> category, close the <nowiki></noinclude></nowiki> tag.
*The documentation sub-page must be titled "Documentation", as in [[Template:Game/Documentation]]. After adding the {{t|Documentation}} template, you can click the red link it creates to go to the properly named page.
*The sub-page should be categorized by <nowiki>[[Category:Template documentation|{{subst:PAGENAME}}]], surrounded by <noinclude> tags</nowiki>.


==Using a template==
==Using a template==
Line 33: Line 42:
hugs, {{{2}}}</small>
hugs, {{{2}}}</small>
</nowiki></pre>  
</nowiki></pre>  
|<code><nowiki>{{Thankyou|all|~~~}}</nowiki></code>
|<code><nowiki>{{Thank you|all|~~~}}</nowiki></code>
|{{Thankyou|all|[[User:Someuser|Someuser]]}}
|{{Thank you|all|[[User:Someuser|Someuser]]}}
|-
|-
| Template with named parameters
| Template with named parameters
Line 42: Line 51:
hugs, {{{signature}}}</small>
hugs, {{{signature}}}</small>
</nowiki></pre>  
</nowiki></pre>  
|<pre><nowiki>{{Thankyou
|<pre><nowiki>{{Thank you
|reason=all
|reason=all
|signature=~~~
|signature=~~~
}}</nowiki></pre>
}}</nowiki></pre>
|{{Thankyou|all|[[User:Someuser|Someuser]]}}
|{{Thank you|all|[[User:Someuser|Someuser]]}}
|}
|}


You can define parameters in templates either numbered as <code><nowiki>{{{1}}}</nowiki></code> or named <code><nowiki>{{{param}}}</nowiki></code>.   
You can define parameters in templates either numbered as <code><nowiki>{{{1}}}</nowiki></code> or named <code><nowiki>{{{param}}}</nowiki></code>.   


'''Example:''' you want a little thank you note you can put other users on their talk page with a reason and your signature. In the [[Template:Thankyou]] you enter your text.
'''Example:''' you want a little thank you note you can put other users on their talk page with a reason and your signature. In the [[Template:Thank you]] you enter your text.


When using the template on a page, you fill in the parameter values, separated by a pipe char (|): <code><nowiki>{{Thankyou|all|~~~}}</nowiki></code> or - if you have used named parameters  <code><nowiki>{{Thankyou|reason=all|signature=~~~}}</nowiki></code>. 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: <code><nowiki>{{Thankyou|2=~~~|1=all}}</nowiki></code>.
When using the template on a page, you fill in the parameter values, separated by a pipe char (|): <code><nowiki>{{Thank you|all|~~~}}</nowiki></code> or - if you have used named parameters  <code><nowiki>{{Thank you|reason=all|signature=~~~}}</nowiki></code>. 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: <code><nowiki>{{Thank you|2=~~~|1=all}}</nowiki></code>.


==Control template inclusion==
==Control template inclusion==
You can control template inclusion by the use of <code><nowiki><noinclude></nowiki></code> and
You can control template inclusion by the use of <code>&lt;noinclude&gt;</code>, <code>&lt;includeonly&gt;</code> and <code>&lt;onlyinclude&gt;</code> tags.
<code><nowiki><includeonly></nowiki></code> tags.


Anything between <code><nowiki><noinclude></nowiki></code> and <code><nowiki></noinclude></nowiki></code> will be processed and
Anything between <code>&lt;noinclude&gt;</code> and <code>&lt;/noinclude&gt;</code> will be processed and displayed only when the page is being viewed directly, not included.
displayed only when the page is being viewed directly, not included.


Possible applications are:
Possible applications are:
Line 67: Line 74:
* Explanatory text about how to use the template
* Explanatory text about how to use the template


The converse is <code><nowiki><includeonly></nowiki></code>. Text between <code><nowiki><includeonly></nowiki></code> and
The converse is <code>&lt;includeonly&gt;</code>. Text between <code>&lt;includeonly&gt;</code> and <code>&lt;/includeonly&gt;</code> 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.
<code><nowiki></includeonly></nowiki></code> 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.
<code>&lt;onlyinclude&gt;</code> is somewhat different. Anything between
<code>&lt;onlyinclude&gt;</code> and
<code>&lt;/onlyinclude&gt;</code> displays everywhere, but everything outside of it is treated as if it was wrapped in
<code>&lt;noinclude&gt;</code> tags (so only the stuff in the onlyinclude is included on other pages).


Note that the usual update problems apply -- if you change the
Note that the usual update problems apply -- if you change the
Line 75: Line 85:
won't be updated until those pages are edited.
won't be updated until those pages are edited.
==Making templates useful==
==Making templates useful==
For templates to be effective users need to find them and be able to use them. Wikis are not user friendly when it comes to finding, using, and working with templates. A simple technique is to have a usage on the template page.
For templates to be effective, users need to find them and be able to use them. When editing, a list of links to all templates used on the page can be found at the bottom of the page, below the edit text box and Save page button. Wikis are not user friendly when it comes to finding, using, and working with templates. A simple technique is to have a usage section on the template page. For example, under a "Usage" heading, you may find a copy/paste-able version of the template, with example content or a short list of the necessary and optional parameters:
For example
{{col|2|begin}}
<div style="display:table; width:auto;">
<div style="text-align:center;font-size:1.2em;text-decoration:underline">Example content</div>
<pre>
{{sidebar
|float=left, right, or none (default: right)
|class=Variant class (e.g. "banner"; default: empty)
|width=Box width (CSS syntax)
|title=Box title
|contents=Box contents
}}
</pre>
{{col|2}}
<div style="text-align:center;font-size:1.2em;text-decoration:underline">Parameter list</div>
<pre>
{{sidebar
|float=
|class=
|width=
|title=
|contents=
}}
</pre>
*'''Necessary parameters'''
**''title'': Box title
**''contents'': Box contents
**''width'': Box width (CSS syntax)
*'''Optional parameters'''
**''float'': left, right, or none (default: right)
**''class'': Variant class (e.g. "banner"; default: empty)
{{col|2|end}}
The user can then simply copy and paste the template for use. Make sure to use '''<nowiki><noinclude></nowiki>''' tags around everything from the end of the tempate code to the end of the page.
== Advanced template usage ==
== Advanced template usage ==
We already covered parameters, think of them as variables (temporarily stored data). MediaWiki allows us to look at those variables and check if they even contain anything. What this lets us do is create templates in which only certain parts appear if the data exists within them, for instance in [[:Template:Infobox]] and [[:Template:Final Fantasy VII/Boss]].
We already covered parameters, think of them as variables (temporarily stored data). MediaWiki allows us to look at those variables and check if they even contain anything. What this lets us do is create templates in which only certain parts appear if the data exists within them, for instance in {{t|Game}} and [[:Template:Final Fantasy VII/Boss]].


The <nowiki>{{#if:}}</nowiki> function is what lets us manipulate wiki code in this way.  We can test for existence within a parameter, then display a certain text if it exists, and a certain text if it doesn't. This way we can display and hide text when empty spaces would normally exist. Other parser functions like <nowiki>{{#if:}}</nowiki> exist and are used for different purposes. Usage of these functions is outlined below.
The <nowiki>{{#if:}}</nowiki> function is what lets us manipulate wiki code in this way.  We can test for existence within a parameter, then display a certain text if it exists, and a certain text if it doesn't. This way we can display and hide text when empty spaces would normally exist. Other parser functions like <nowiki>{{#if:}}</nowiki> exist and are used for different purposes. Usage of these functions is outlined below.
Note: use <nowiki>{{!}}</nowiki> to replace the use of pipes (the "|" character). If you don't, the conditional argument (e.g. <nowiki>{{#if:}}</nowiki>) will end abruptly.  Typically, <nowiki>{{!}}</nowiki> is used when generating new rows of a table (HTML TR/TD) within a conditional argument.


=== <nowiki>{{#if:}}</nowiki> ===
=== <nowiki>{{#if:}}</nowiki> ===
Line 275: Line 316:


=== <nowiki>{{#switch:}}</nowiki> ===
=== <nowiki>{{#switch:}}</nowiki> ===
This function allows you to do various things depending on the value of a variable (so you only need one function instead of multiple nested ifeqs). The usage for <nowiki>{{#switch:}}</nowiki> is as follows:
<pre>{{#switch:<parameter to test>
|<value 1> = <what to display if parameter is value 1>
|<value 2> = <what to display if parameter is value 2>
|<value 3> = <what to display if parameter is value 3>
...
|<default if none of the above worked>
}}</pre>
Please note that you may have as many checks as you wish and the default parameter is optional. If you need an equals sign <tt>=</tt> in your default value, use <tt>|#default = <default if none of the above worked></tt> instead. Also, the default parameter, if given, must ''always'' be last. Comparisons are case sensitive. "Default" parameters within the statements themselves will just be read as the value to match, and the text displayed will be the next parameter with the equals sign.
{|{{prettytable}}
|+ Usage of <nowiki>{{#switch:}}</nowiki>
|-
!You type…
!You see…
|-
|<pre>{{#switch:hello|HELLO=foo|Hello=bar|baz}}</pre>
|{{#switch:hello|HELLO=foo|Hello=bar|baz}}
|-
|<pre>{{#switch:hello|HELLO=foo|hello=bar|baz}}</pre>
|{{#switch:hello|HELLO=foo|hello=bar|baz}}
|-
|<pre>{{#switch:hello|HELLO=foo = 1|Hello=bar = 2|#default=baz = 3}}</pre>
|{{#switch:hello|HELLO=foo = 1|Hello=bar = 2|#default=baz = 3}}
|-
|<pre>{{#switch:hello|HELLO=foo = 1|hello|hi|Hello=bar = 2|#default=baz = 3}}</pre>
|{{#switch:hello|HELLO=foo = 1|hello|hi|Hello=bar = 2|#default=baz = 3}}
|-
|<pre>{{#switch:{{{parametername}}}|<value1>=Whatever|<value2>=Something|Nothing}}</pre>
|When used in a template, depending on what you pass to "{{{parametername}}}", it could be either "Whatever", "Something", or "Nothing" if nothing is passed.
|}


{{Footer Nav|game=StrategyWiki:Guide|prevpage=Tables|nextpage=Preview and save}}
{{Footer Nav|game=StrategyWiki:Guide|prevpage=Regular expressions|nextpage=Walkthrough}}
</div>