From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search
(→‎Advanced Template Usage: #ifeq documentation)
m (Help:Template moved to StrategyWiki:Guide/Templates: merging edit histories)
Line 1: Line 1:
{{All Game Nav|game=StrategyWiki:Guide}}
'''Templates''' are pre-created content that you can easily drop into wiki pages.
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==
== Using Templates ==
Template names are prefixed with <code>Template:</code>, you create them like any other wiki page.
Inserting a template into a page is easy. Simply surround the template name with two curly brackets on each side, like this:


==Using a template==
<pre>{{example}}</pre>
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


==Using parameters in templates==
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.
<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>.


'''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.
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:


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


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


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


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


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


Note that the usual update problems apply -- if you change the
<pre>{{example
categories inside a template, the categories of the referring pages
|field1=ABC
won't be updated until those pages are edited.
|field2=DEF
==Making templates useful==
}}</pre>
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]].


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


===<nowiki>{{#if:}}</nowiki>===
== Finding Templates ==
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:
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.
<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:"


===<nowiki>{{#expr:}}</nowiki>===
<pre>Template:example</pre>
<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.


{| {{prettytable}}
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.
|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}}
|}


===<nowiki>{{#ifeq:}}</nowiki>===
To put in changeable fields, surround the name of the field with three curly brackets:
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>


{| {{prettytable}}
<pre>{{{fieldname}}}</pre>
|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}}
|}


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


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]].
You can also create "nameless" fields by using numbers:


== See also ==
<pre>{{{1}}}</pre>
*[[:Category:Templates]]
 
{{Footer Nav|game=StrategyWiki:Guide|backpage=Tables|nextpage=Preview and save}}
The numbers will be filled in with content in order.
</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:07, 13 May 2007

Templates are pre-created content that you can easily drop into wiki pages.

Using Templates

Inserting a template into a page is easy. Simply surround the template name with two curly brackets on each side, like this:

{{example}}

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.

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|field1=ABC|field2=DEF}}

This places the text "ABC" into the field called "field1" and "DEF" into the field called "field2".

Sometimes you do not need to name the field:

{{example|ABC|DEF}}

You can also break up the fields onto multiple lines for readability:

{{example
|field1=ABC
|field2=DEF
}}

Some useful templates

Finding Templates

Templates exist in the "Template" namespace. You can see all existing templates by viewing the Allpages special page and selecting the Template namespace.

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:"

Template:example

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.

To put in changeable fields, surround the name of the field with three curly brackets:

{{{fieldname}}}

This allows people to fill in that space with their own content when using your template.

You can also create "nameless" fields by using numbers:

{{{1}}}

The numbers will be filled in with content in order.

External Help

http://meta.wikimedia.org/wiki/Help:Template_namespace - Mediawiki documentation http://home.comcast.net/~gerisch/MediaWikiTemplates.html - Brief tutorial