Help:Templates: Difference between revisions

From SmashWiki, the Super Smash Bros. wiki
Jump to navigationJump to search
(→‎Template parameters: calling. might use examples)
(A little further on this.)
Line 8: Line 8:


===Template parameters===
===Template parameters===
Sometimes, a template will have required or optional parameters that need to be specified on the template's use. There are 2 methods of supplying parameters (which one should be used depends on the template in question):
Sometimes, a template will have required or optional parameters that need to be specified on the template's use. There are two methods of supplying parameters (which one should be used depends on the template in question):
# Supplying parameters in a unnamed list. This is typically done with templates with a low number of parameters, such as {{t|SSB}}. To do this, simply add a bar ("|") after the template name, followed by the parameter's contents. This is to follow a <nowiki>{{TemplateName|Param1|Param2|Param3...}}</nowiki> format.
# Supplying parameters in a unnamed list. This is typically done with templates with a low number of parameters, such as {{t|SSB}}. To do this, simply add a bar ("|") after the template name, followed by the parameter's contents. This is to follow a <nowiki>{{TemplateName|Param1|Param2|Param3...}}</nowiki> format.
#:'''Example''': <nowiki>{{SSB|Mario}}</nowiki> returns {{SSB|Mario}}
#:'''Example''': <nowiki>{{SSB|Mario}}</nowiki> returns {{SSB|Mario}}
Line 14: Line 14:
#:'''Example''': <nowiki>{{Head|Mario|g=SSB|s=16px}}</nowiki> returns {{Head|Mario|g=SSB|s=16px}}
#:'''Example''': <nowiki>{{Head|Mario|g=SSB|s=16px}}</nowiki> returns {{Head|Mario|g=SSB|s=16px}}
#:<small>*Note that this example actually uses both parameter methods.</small>
#:<small>*Note that this example actually uses both parameter methods.</small>
Parameters can then be used inside the template itself with the syntax <code><nowiki>{{{parameter name/number|text to be inserted if the parameter is not supplied}}}</nowiki></code>.


===Substitution===
===Substitution===
Substitution is a different form of transclusion in which the template is "written" directly to the page when used (that is, the text that the template produces actually gets inserted where substituted). If a template is substituted, the page the template is used on will not be added to the template's "What links here" page as it would if a normal transclusion occurred.
*The most used template that should be substituted is {{t|Welcome}}


==Building templates==
==Building templates==
Templates can be created by making a page in the template namespace. To do this, simply create Template:TemplateName.
Templates can be created by making a page in the template namespace. To do this, simply create Template:TemplateName.
===Parameters===
===Parameters===
Parameters can be included in the template in order to give specific directions to the template. They can be required parameters, or they can be optional and have default values. They can be defined in two ways:
*'''Named parameters''' are usually used in templates that have two or more parameters associated with them. They can be defined as follows:<br>
<code><nowiki>{{{<name>|<defaultvalue>}}}</nowiki></code>
*'''Unnamed parameters''' are usually used in templates that have only one parameter or as a main parameter (through with name parameters build off of afterwards). They can be defined as follows:<br>
<code><nowiki>{{{<numberofappearance>|<defaultvalue>}}}</nowiki></code>
====Examples====
{| class=wikitable
!colspan=3|Template:Fruit
|-
!Template contents!!Usage!!Output
|-
|<code><nowiki>Today's fruit is a(n) {{{fruit|apple}}}.</nowiki></code>||<code><nowiki>{{Fruit}}</nowiki></code>||Today's fruit is a(n) apple.
|-
|<code><nowiki>Today's fruit is a(n) {{{fruit|apple}}}.</nowiki></code>||<code><nowiki>{{Fruit|fruit=orange}}</nowiki></code>||Today's fruit is a(n) orange.
|-
|<code><nowiki>Today's fruit is a(n) {{{fruit}}}.</nowiki></code>||<code><nowiki>{{Fruit}}</nowiki></code>||Today's fruit is a(n) <nowiki>{{{fruit}}}</nowiki>.
|-
|<code><nowiki>Today's fruit is a(n) {{{1|apple}}}.</nowiki></code>||<code><nowiki>{{Fruit}}</nowiki></code>||Today's fruit is a(n) apple.
|-
|<code><nowiki>Today's fruit is a(n) {{{1|apple}}}. It is {{{2|red}}}.</nowiki></code>||<code><nowiki>{{Fruit|grape|purple}}</nowiki></code>||Today's fruit is a(n) grape. It is purple.
|-
|<code><nowiki>Today's fruit is a(n) {{{1|apple}}}. It is {{{color|red}}}.</nowiki></code>||<code><nowiki>{{Fruit|grape|color=purple}}</nowiki></code>||Today's fruit is a(n) grape. It is purple.
|}
===Conditionals and expressions===
===Conditionals and expressions===
Conditionals are expressions of satisfaction; they are if-then statements. Basically, a condition is given, and if that condition is satisfied, something happens.
====#if====
====#if====
The most basic conditional test. #if is used to test for content in the first "parameter". If something is there, the second "parameter" gets outputted. If null, the third "parameter" gets outputted. The most common usage for this in templates is to test for null parameters. This is accomplished by wrapping the parameter in the #if conditional and making it's default value null, like so:<br><code><nowiki>{{#if:{{{<parameter>|}}}|<ifnotnull>|<ifnull>}}</nowiki></code>
=====Examples=====
{| class=wikitable
!colspan=3|Template:Fruit
|-
!Template contents!!Usage!!Output
|-
|&mdash;||<code><nowiki>{{#if:Something|It's not null|It's null}}</nowiki></code>||It's not null
|-
|&mdash;||<code><nowiki>{{#if:|It's not null|It's null}}</nowiki></code>||It's null
|-
|<code><nowiki>There is {{#if:{{{1|}}}|fruit|no fruit}}.</nowiki></code>||<code><nowiki>{{fruit}}</nowiki></code>||There is no fruit.
|-
|<code><nowiki>There is {{#if:{{{1|}}}|fruit|no fruit}}.</nowiki></code>||<code><nowiki>{{fruit|apple}}</nowiki></code>||There is fruit.
|-
|<code><nowiki>There is{{#if:{{{1|}}}||&nbsp;no}} fruit.</nowiki></code>||<code><nowiki>{{fruit}}</nowiki></code>||There is&nbsp;no fruit.
|}
====#ifeq====
====#ifeq====
====#iferror====
====#iferror====

Revision as of 21:08, February 27, 2016

Proposed.png
This page is a draft.
Feel free to contribute to this page as you would a mainspace page.

Templates are tools used in our articles that serve to both provide consistency and simplify the editing process. They may be used to quickly and easily build tables or infoboxes, or they could be used as link shortcuts. For a list of usable templates, see Category:Templates.

Using templates

To use a template, the following syntax should be used: {{TemplateName}}. This is called "transclusion". For example, to use the template {{example}}, one would insert {{example}} in the place where the template should be included. The result will be the contents of Template:Example:

Correct
The template was successfully included in this page.

Template parameters

Sometimes, a template will have required or optional parameters that need to be specified on the template's use. There are two methods of supplying parameters (which one should be used depends on the template in question):

  1. Supplying parameters in a unnamed list. This is typically done with templates with a low number of parameters, such as {{SSB}}. To do this, simply add a bar ("|") after the template name, followed by the parameter's contents. This is to follow a {{TemplateName|Param1|Param2|Param3...}} format.
    Example: {{SSB|Mario}} returns Mario
  2. Supplying named parameters. This is usually used with templates the supply many parameters, such as {{Head}}. To do this, still add the bar, but instead of inserting the parameter's contents right away, add the parameter name (specified by the template) followed by an equals sign ("="), then the parameter's contents.
    Example: {{Head|Mario|g=SSB|s=16px}} returns Mario (SSB)
    *Note that this example actually uses both parameter methods.

Substitution

Substitution is a different form of transclusion in which the template is "written" directly to the page when used (that is, the text that the template produces actually gets inserted where substituted). If a template is substituted, the page the template is used on will not be added to the template's "What links here" page as it would if a normal transclusion occurred.

  • The most used template that should be substituted is {{Welcome}}

Building templates

Templates can be created by making a page in the template namespace. To do this, simply create Template:TemplateName.

Parameters

Parameters can be included in the template in order to give specific directions to the template. They can be required parameters, or they can be optional and have default values. They can be defined in two ways:

  • Named parameters are usually used in templates that have two or more parameters associated with them. They can be defined as follows:

{{{<name>|<defaultvalue>}}}

  • Unnamed parameters are usually used in templates that have only one parameter or as a main parameter (through with name parameters build off of afterwards). They can be defined as follows:

{{{<numberofappearance>|<defaultvalue>}}}

Examples

Template:Fruit
Template contents Usage Output
Today's fruit is a(n) {{{fruit|apple}}}. {{Fruit}} Today's fruit is a(n) apple.
Today's fruit is a(n) {{{fruit|apple}}}. {{Fruit|fruit=orange}} Today's fruit is a(n) orange.
Today's fruit is a(n) {{{fruit}}}. {{Fruit}} Today's fruit is a(n) {{{fruit}}}.
Today's fruit is a(n) {{{1|apple}}}. {{Fruit}} Today's fruit is a(n) apple.
Today's fruit is a(n) {{{1|apple}}}. It is {{{2|red}}}. {{Fruit|grape|purple}} Today's fruit is a(n) grape. It is purple.
Today's fruit is a(n) {{{1|apple}}}. It is {{{color|red}}}. {{Fruit|grape|color=purple}} Today's fruit is a(n) grape. It is purple.

Conditionals and expressions

Conditionals are expressions of satisfaction; they are if-then statements. Basically, a condition is given, and if that condition is satisfied, something happens.

#if

The most basic conditional test. #if is used to test for content in the first "parameter". If something is there, the second "parameter" gets outputted. If null, the third "parameter" gets outputted. The most common usage for this in templates is to test for null parameters. This is accomplished by wrapping the parameter in the #if conditional and making it's default value null, like so:
{{#if:{{{<parameter>|}}}|<ifnotnull>|<ifnull>}}

Examples
Template:Fruit
Template contents Usage Output
{{#if:Something|It's not null|It's null}} It's not null
{{#if:|It's not null|It's null}} It's null
There is {{#if:{{{1|}}}|fruit|no fruit}}. {{fruit}} There is no fruit.
There is {{#if:{{{1|}}}|fruit|no fruit}}. {{fruit|apple}} There is fruit.
There is{{#if:{{{1|}}}|| no}} fruit. {{fruit}} There is no fruit.

#ifeq

#iferror

#ifexpr

#ifexist

#switch

#expr

Inclusion tags

includeonly

noinclude

onlyinclude