From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search
m (Help:Template moved to StrategyWiki:Guide/Templates: merging edit histories)
(restoring this revision after merging histories)
Line 1: Line 1:
'''Templates''' are pre-created content that you can easily drop into wiki pages.
{{All Game Nav|game=StrategyWiki:Guide}}
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).


== Using Templates ==
==Creating a template==
Inserting a template into a page is easy. Simply surround the template name with two curly brackets on each side, like this:
Template names are prefixed with <code>Template:</code>, you create them like any other wiki page.


<pre>{{example}}</pre>
==Using a template==
Templates are wiki pages which can be used in other pages in three ways:
:<code><nowiki>{{Name}}</nowiki></code> includes the current content of the page <nowiki>"[[Template:Name]]"</nowiki>.
:<code><nowiki>{{subst:Name}}</nowiki></code> inserts the content of the page <nowiki>"[[Template:Name]]"</nowiki> when you save your text.  Subst will use the template one time for the creation of the page; afterwards if the template is updated, the page that subst:template was used on will not have those changes made to them.
:<code><nowiki>{{msgnw:Name}}</nowiki></code> includes the template in raw wiki syntax, like <code><nowiki>&lt;nowiki&gt;</nowiki></code> does


This will insert the contents of the template named "example". Note that if a template is modified after it has been placed in pages, its appearance will be updated on all of the pages that use it.
==Using parameters in templates==
<div style="float:right">
{| {{Prettytable}}
|-
|bgcolor="#A7C1F2" colspan="2" align="center" |'''Template with numbered parameters'''
|-
| colspan="2" |
<pre><nowiki>
'''A little thank you...'''<br>
<small>for {{{1}}}.<br>
hugs, {{{2}}}</small>
</nowiki></pre>
|-
|bgcolor="#A7C1F2"|'''You type'''
|bgcolor="#A7C1F2"|'''You get'''
|-
|<code><nowiki>{{Thankyou|all|~~~}}</nowiki></code>
|
{{Thankyou|all|[[User:Someuser|Someuser]]}}
|-
|bgcolor="#A7C1F2" colspan="2" align="center" |'''with named parameters'''
|-
| colspan="2" |
<pre><nowiki>
'''A little thank you...'''<br>
<small>for {{{reason}}}.<br>
hugs, {{{signature}}}</small>
</nowiki></pre>
|-
|bgcolor="#A7C1F2"|'''You type'''
|bgcolor="#A7C1F2"|'''You get'''
|-
|<pre><nowiki>{{Thankyou
|reason=all
|signature=~~~}}</nowiki></pre>
|
{{Thankyou|all|[[User:Someuser|Someuser]]}}
|}
</div>
You can define parameters in templates either numbered as <code><nowiki>{{{1}}}</nowiki></code> or named <code><nowiki>{{{param}}}</nowiki></code>.


Some templates allow you to insert your own content into the template's body. Use the following format if the template you want to use has changeable fields:
'''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.


<pre>{{example|field1=ABC|field2=DEF}}</pre>
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>.


This places the text "ABC" into the field called "field1" and "DEF" into the field called "field2".
==Control template inclusion==
You can control template inclusion by the use of <code><nowiki><noinclude></nowiki></code> and
<code><nowiki><includeonly></nowiki></code> tags.


Sometimes you do not need to name the field:
Anything between <code><nowiki><noinclude></nowiki></code> and <code><nowiki></noinclude></nowiki></code> will be processed and
displayed only when the page is being viewed directly, not included.


<pre>{{example|ABC|DEF}}</pre>
Possible applications are:
* Categorising templates
* Interlanguage links to similar templates in other languages
* Explanatory text about how to use the template


You can also break up the fields onto multiple lines for readability:
The converse is <code><nowiki><includeonly></nowiki></code>. Text between <code><nowiki><includeonly></nowiki></code> and
<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.


<pre>{{example
Note that the usual update problems apply -- if you change the
|field1=ABC
categories inside a template, the categories of the referring pages
|field2=DEF
won't be updated until those pages are edited.
}}</pre>
==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 example
<div style="display:table; width:auto;">
==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]].


=== Some useful templates ===
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.
* [[Template:Spoilers]]: Warn users of upcoming spoilers
* [[Template:Infobox]]: Insert "at-a-glance" game information.
* [[Template:Copyvio]]: Mark page as a possible copyright violation.


== Finding Templates ==
===<nowiki>{{#if:}}</nowiki>===
Templates exist in the "Template" namespace. You can see all existing templates by viewing the [[Special:Allpages|Allpages]] special page and selecting the Template namespace.
The {{#if:}} function allows us to test a variable to see if it is empty or not. It returns the first value (value after the first pipe) if it is not empty and the second value (value after the second pipe) if it is. It is used with the syntax:
<pre>{{#if:<variable to test>|<what to display if test is not empty>|<what to display if test is empty>}}</pre>
{| {{prettytable|notwide=1}}
|bgcolor="#A7C1F2" colspan="3" align="center"|'''Usage of <nowiki>{{#if:}}</nowiki>'''
|-
!What you type
!What you see
!Why you see it
|-
|<pre>{{#if:{{{1|}}}|not empty|empty}}</pre>
|empty
|There is no defined value for the variable <nowiki>{{{1}}}</nowiki>, so it jumps to the default value after the pipe character (which is nothing in this case). This default value of nothing returns the empty value.
|-
|<pre>{{#if:{{{1|a}}}|not empty|empty}}</pre>
|not empty
|Because <nowiki>{{{1}}}</nowiki> is again not defined, so it jumps to the default value of "a". Because "a" is a non-empty string, <nowiki>{{#if:}}</nowiki> returns the not empty value.
|-
|<pre>{{#if:|not empty|empty}}</pre>
|empty
|The parameter being tested is empty, so it returns that value.
|-
|<pre>{{#if:hello|not empty|empty}}</pre>
|not empty
|The string "hello" is not an empty string, so it returns the not empty value.
|-
|<pre>{{#if:hello|not empty}}</pre>
|not empty
|It is possible to skip unneeded parts of the #if construct, in this case, the <what to display if test is empty> part.
|-
|<pre>{{#if:||empty}}</pre>
|empty
|The test parameter was empty, so that was the result. When skipping sections in the middle of the #if construct, you still need the pipes to go to later sections.
|}


== Creating/Editing Templates ==
To create or edit a template, enter the name of the template into the search box and press Go. Remember that templates are in the "Template" namespace, so  you must prefix your template name with "Template:"


<pre>Template:example</pre>
===<nowiki>{{#expr:}}</nowiki>===
<nowiki>{{#expr:}}</nowiki> evaluates a mathematical expression or boolean comparison (where the results are only 1 or 0, for true and false respectively). It is used with the syntax:<pre>
{{#expr:<expression to be evaluated>}}</pre>
The operands that can be used in <nowiki>{{#expr:}}</nowiki> are listed below.


You can edit a template just like any other page. Make your changes in the edit box and then save them to update the template. This will change the appearance of the template throughout the whole wiki, including on pages which already use the template.
{| {{prettytable}}
|bgcolor="#A7C1F2" colspan="3" align="center"|'''Usage of <nowiki>{{#expr:}}</nowiki>'''
|-
! ''Operator''
! ''Operation''
! ''Example''
|-
|rowspan="2" colspan="2" align="center"| none
|
<nowiki>{{#expr: 123456789012345}}</nowiki> = {{#expr: 123456789012345}}
|-
|
<nowiki>{{#expr: 0.000001}}</nowiki> = {{#expr: 0.000001}}
|-
! ( )
| Grouping operators
|
<nowiki>{{#expr: (30 + 7) * 7 }}</nowiki> = {{#expr: (30 + 7) * 7 }}
|-
! +
| Unary '''<tt>+</tt>''' sign
|
<nowiki>{{#expr: +30 * +7}}</nowiki> = {{#expr: +30 * +7}}
|-
! -
| Unary '''<tt>-</tt>''' sign (negation)
|
<nowiki>{{#expr: -30 * -7}}</nowiki> = {{#expr: -30 * -7}}
|-
! not
|  Unary NOT, logical NOT
|
<nowiki>{{#expr: not 0 * 7}}</nowiki> = {{#expr: not 0 * 7}}<br />
<nowiki>{{#expr: not 30+7}}</nowiki> = {{#expr: not 30+7}}
|-
! *
| Multiplication
|
<nowiki>{{#expr: 30 * 7}}</nowiki> = {{#expr: 30 * 7}}
|-
! /
| Division, same as '''div'''
|
<nowiki>{{#expr: 30 / 7}}</nowiki> = {{#expr: 30 / 7}}
|-
! div
| Division, same as '''/''',<br />no integer division
|
<nowiki>{{#expr: 30 div 7}}</nowiki> = {{#expr: 30 div 7}}<br />
<nowiki>{{#expr: 5 div 2 * 2 + 5 mod 2}}</nowiki> = {{#expr: 5 div 2 * 2 + 5 mod 2}}
|-
! mod
| "Modulo", remainder of division after truncating both operands to an integer.<br />Caveat, '''div''' and '''mod''' are different from all programming languages.
|
<nowiki>{{#expr: 30 mod 7}}</nowiki> = {{#expr: 30 mod 7}}<br />
<nowiki>{{#expr: -8 mod -3}}</nowiki> = {{#expr: -8 mod -3}}<br />
<nowiki>{{#expr: -8 mod +3}}</nowiki> = {{#expr: -8 mod +3}}<br />
<nowiki>{{#expr: 8 mod 2.7}}</nowiki> = {{#expr: 8 mod 2.7}}<br />
<nowiki>{{#expr: 8 mod 3.2}}</nowiki> = {{#expr: 8 mod 3.2}}<br />
<nowiki>{{#expr: 8.9 mod 3}}</nowiki> = {{#expr: 8.9 mod 3}}
|-
! <tt>+</tt>
| Addition
|
<nowiki>{{#expr: 30 + 7}}</nowiki> = {{#expr: 30 + 7}}
|-
! <tt>-</tt>
| Subtraction
|
<nowiki>{{#expr: 30 - 7}}</nowiki> = {{#expr: 30 - 7}}
|-
! round
| Rounds off the number on the left to the power of 1/10 given on the right
|
<nowiki>{{#expr: 30 / 7 round 3}}</nowiki> = {{#expr: 30 / 7 round 3}}<br />
<nowiki>{{#expr: 30 / 7 round 0}}</nowiki> = {{#expr: 30 / 7 round 0}}<br />
<nowiki>{{#expr: 3456 round -2}}</nowiki> = {{#expr: 3456 round -2}}
|-
! =
| Equality (numerical incl. logical)
|
<nowiki>{{#expr: 30 = 7}}</nowiki> = {{#expr: 30 = 7}}
|-
! &lt;&gt;
| Inequality, same as '''!='''
|
<nowiki>{{#expr: 30 <> 7}}</nowiki> = {{#expr: 30 <> 7}}
|-
! !=
| Inequality, same as '''&lt;&gt;''', logical ''xor''
|
<nowiki>{{#expr: 1 != 0}}</nowiki> = {{#expr: 1 != 0}}
|-
! &lt;
| Less than
|
<nowiki>{{#expr: 30 < 7}}</nowiki> = {{#expr: 30 < 7}}
|-
! &gt;
| Greater than
|
<nowiki>{{#expr: 30 > 7}}</nowiki> = {{#expr: 30 > 7}}
|-
! &lt;=
| Less than or equal to
|
<nowiki>{{#expr: 30 <= 7}}</nowiki> = {{#expr: 30 <= 7}}
|-
! &gt;=
| Greater than or equal to
|
<nowiki>{{#expr: 30 >= 7}}</nowiki> = {{#expr: 30 >= 7}}
|-
! and
| Logical AND
|
<nowiki>{{#expr: 4<5 and 4 mod 2}}</nowiki> = {{#expr: 4<5 and 4 mod 2}}
|-
! or
| Logical OR
|
<nowiki>{{#expr: 4<5 or 4 mod 2}}</nowiki> = {{#expr: 4<5 or 4 mod 2}}
|}


To put in changeable fields, surround the name of the field with three curly brackets:
===<nowiki>{{#ifeq:}}</nowiki>===
This function compares a string or numerical value in <parameter to test> to the string or numerical value in <parameter to compare test to>. If you wish to compare strings represented by numbers and numeral operators, enclose the string within "" to force a string comparison. <What to display if test is true> and <what to display if test is false> are optional, but one must be used unless you wish to return absolutely nothing. Also, when testing operations, use <nowiki>{{#expr:}}</nowiki>. The usage for <nowiki>{{#ifeq:}}</nowiki> is as follows:
<pre>{{#ifeq:<parameter to test>
|<parameter to compare test to>
|<what to display if test is true>
|<what to display if test is false>}}</pre>


<pre>{{{fieldname}}}</pre>
{| {{prettytable}}
|bgcolor="#A7C1F2" colspan="2" align="center"|'''Usage of <nowiki>{{#ifeq:}}</nowiki>'''
|-
!What you type
!What is displayed
|-
|<pre>{{#ifeq:+07|007|yes|no}}</pre>
|{{#ifeq:+07|007|yes|no}}
|-
|<pre>{{#ifeq:"+07"|"007"|yes|no}}</pre>
|{{#ifeq:"+07"|"007"|yes|no}}
|-
|<pre>{{#ifeq:foo|bar|yes|no}}</pre>
|{{#ifeq:foo|bar|yes|no}}
|-
|<pre>{{#ifeq:{{#expr:7*5+5}}|40|yes|no}}</pre>
|{{#ifeq:{{#expr:7*5+5}}|40|yes|no}}
|}


This allows people to fill in that space with their own content when using your template.
==Usage==
Allows to establish a link to a subject:
<div><pre>
{{NameOfTemplate|Term1+Term2+Term3}}
</pre></div>


You can also create "nameless" fields by using numbers:
The user can simply copy and paste the template to establish a new page that is similar to other pages. The example demonstrated is trival true, however a working example that demonstrates how templates can be used in complex situations to save time, accuracy and help categories pages on specialty topics more easily. see working example [[Template:All Game Nav|here]].


<pre>{{{1}}}</pre>
== See also ==
 
*[[:Category:Templates]]
The numbers will be filled in with content in order.
{{Footer Nav|game=StrategyWiki:Guide|backpage=Tables|nextpage=Preview and save}}
 
</div>
== External Help ==
http://meta.wikimedia.org/wiki/Help:Template_namespace - Mediawiki documentation
http://home.comcast.net/~gerisch/MediaWikiTemplates.html - Brief tutorial

Revision as of 05:09, 13 May 2007

Template:All Game Nav 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).

Creating a template

Template names are prefixed with Template:, you create them like any other wiki page.

Using a template

Templates are wiki pages which can be used in other pages in three ways:

{{Name}} includes the current content of the page "[[Template:Name]]".
{{subst:Name}} inserts the content of the page "[[Template:Name]]" when you save your text. Subst will use the template one time for the creation of the page; afterwards if the template is updated, the page that subst:template was used on will not have those changes made to them.
{{msgnw:Name}} includes the template in raw wiki syntax, like <nowiki> does

Using parameters in templates

Template with numbered parameters
 
'''A little thank you...'''<br>
<small>for {{{1}}}.<br>
hugs, {{{2}}}</small>
You type You get
{{Thankyou|all|~~~}}

Template:Thankyou

with named parameters
 
'''A little thank you...'''<br>
<small>for {{{reason}}}.<br>
hugs, {{{signature}}}</small>
You type You get
{{Thankyou
|reason=all
|signature=~~~}}

Template:Thankyou

You can define parameters in templates either numbered as {{{1}}} or named {{{param}}}.

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.

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

Control template inclusion

You can control template inclusion by the use of <noinclude> and <includeonly> tags.

Anything between <noinclude> and </noinclude> will be processed and displayed only when the page is being viewed directly, not included.

Possible applications are:

  • Categorising templates
  • Interlanguage links to similar templates in other languages
  • Explanatory text about how to use the template

The converse is <includeonly>. Text between <includeonly> and </includeonly> 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.

Note that the usual update problems apply -- if you change the categories inside a template, the categories of the referring pages won't be updated until those pages are edited.

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 example

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.

The {{#if:}} 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 {{#if:}} exist and are used for different purposes. Usage of these functions is outlined below.

{{#if:}}

The function allows us to test a variable to see if it is empty or not. It returns the first value (value after the first pipe) if it is not empty and the second value (value after the second pipe) if it is. It is used with the syntax:

{{#if:<variable to test>|<what to display if test is not empty>|<what to display if test is empty>}}
Usage of {{#if:}}
What you type What you see Why you see it
{{#if:{{{1|}}}|not empty|empty}}
empty There is no defined value for the variable {{{1}}}, so it jumps to the default value after the pipe character (which is nothing in this case). This default value of nothing returns the empty value.
{{#if:{{{1|a}}}|not empty|empty}}
not empty Because {{{1}}} is again not defined, so it jumps to the default value of "a". Because "a" is a non-empty string, {{#if:}} returns the not empty value.
{{#if:|not empty|empty}}
empty The parameter being tested is empty, so it returns that value.
{{#if:hello|not empty|empty}}
not empty The string "hello" is not an empty string, so it returns the not empty value.
{{#if:hello|not empty}}
not empty It is possible to skip unneeded parts of the #if construct, in this case, the <what to display if test is empty> part.
{{#if:||empty}}
empty The test parameter was empty, so that was the result. When skipping sections in the middle of the #if construct, you still need the pipes to go to later sections.


{{#expr:}}

{{#expr:}} evaluates a mathematical expression or boolean comparison (where the results are only 1 or 0, for true and false respectively). It is used with the syntax:
{{#expr:<expression to be evaluated>}}

The operands that can be used in {{#expr:}} are listed below.

Usage of {{#expr:}}
Operator Operation Example
none

{{#expr: 123456789012345}} = 1.2345678901234E+14

{{#expr: 0.000001}} = 1.0E-6

( ) Grouping operators

{{#expr: (30 + 7) * 7 }} = 259

+ Unary + sign

{{#expr: +30 * +7}} = 210

- Unary - sign (negation)

{{#expr: -30 * -7}} = 210

not Unary NOT, logical NOT

{{#expr: not 0 * 7}} = 7
{{#expr: not 30+7}} = 7

* Multiplication

{{#expr: 30 * 7}} = 210

/ Division, same as div

{{#expr: 30 / 7}} = 4.2857142857143

div Division, same as /,
no integer division

{{#expr: 30 div 7}} = 4.2857142857143
{{#expr: 5 div 2 * 2 + 5 mod 2}} = 6

mod "Modulo", remainder of division after truncating both operands to an integer.
Caveat, div and mod are different from all programming languages.

{{#expr: 30 mod 7}} = 2
{{#expr: -8 mod -3}} = -2
{{#expr: -8 mod +3}} = -2
{{#expr: 8 mod 2.7}} = 0
{{#expr: 8 mod 3.2}} = 2
{{#expr: 8.9 mod 3}} = 2

+ Addition

{{#expr: 30 + 7}} = 37

- Subtraction

{{#expr: 30 - 7}} = 23

round Rounds off the number on the left to the power of 1/10 given on the right

{{#expr: 30 / 7 round 3}} = 4.286
{{#expr: 30 / 7 round 0}} = 4
{{#expr: 3456 round -2}} = 3500

= Equality (numerical incl. logical)

{{#expr: 30 = 7}} = 0

<> Inequality, same as !=

{{#expr: 30 <> 7}} = 1

!= Inequality, same as <>, logical xor

{{#expr: 1 != 0}} = 1

< Less than

{{#expr: 30 < 7}} = 0

> Greater than

{{#expr: 30 > 7}} = 1

<= Less than or equal to

{{#expr: 30 <= 7}} = 0

>= Greater than or equal to

{{#expr: 30 >= 7}} = 1

and Logical AND

{{#expr: 4<5 and 4 mod 2}} = 0

or Logical OR

{{#expr: 4<5 or 4 mod 2}} = 1

{{#ifeq:}}

This function compares a string or numerical value in <parameter to test> to the string or numerical value in <parameter to compare test to>. If you wish to compare strings represented by numbers and numeral operators, enclose the string within "" to force a string comparison. <What to display if test is true> and <what to display if test is false> are optional, but one must be used unless you wish to return absolutely nothing. Also, when testing operations, use {{#expr:}}. The usage for {{#ifeq:}} is as follows:

{{#ifeq:<parameter to test>
|<parameter to compare test to>
|<what to display if test is true>
|<what to display if test is false>}}
Usage of {{#ifeq:}}
What you type What is displayed
{{#ifeq:+07|007|yes|no}}
yes
{{#ifeq:"+07"|"007"|yes|no}}
no
{{#ifeq:foo|bar|yes|no}}
no
{{#ifeq:{{#expr:7*5+5}}|40|yes|no}}
yes

Usage

Allows to establish a link to a subject:

{{NameOfTemplate|Term1+Term2+Term3}}

The user can simply copy and paste the template to establish a new page that is similar to other pages. The example demonstrated is trival true, however a working example that demonstrates how templates can be used in complex situations to save time, accuracy and help categories pages on specialty topics more easily. see working example here.

See also