coding help

Template parameters are a basic feature of templates on wikis. They allow users to customize templates when using them. When someone writes code for a template, they can include parameters.

Parameters let you specify details about a template. Each template may have 0, a few, or many parameters included. A template's page should tell you about your parameter options. Usually, template creators and editors use template data to describe your options.

Templates are similar to functions in programming.[1] (They take in input and produce an output.)

If you don't know what templates are, we recommend you learn about them first. Once you understand the basics, come back here.

Specifying parameters while using userboxes

Userbox example

Let's use the userbox template {{User loves Coding Help Wiki}} as an example. If you'd like, open it in a new tab or window as you follow along. You could even try testing it in your sandbox.

The userbox's template page describes a few parameters. They only accept certain inputs. (This is due to use of a #switch parser function, which we won't get into right now.) The parameters we'll explore are:

Note that parameter names are case sensitive.[2] So "color" will work but "Color" and "COLOR" won't.

Its source editor code is {{User loves Coding Help Wiki}}. When we type this in source mode, the result is:


Let's say we want a different color instead of blue. The page lists color options. Purple is one of them, so let's try that. Here's where we'll use a parameter. In the visual editor, you edit template parameters through the template data dialog box. In source mode, it looks like this:

{{User loves Coding Help Wiki|color=Purple}}

We use a pipe (|) as a separator, then the parameter name (color) with an equals sign to choose the value (Purple). And the result is:


In a template data dialog box, a user edits the User Loves Coding Help Wiki template's parameters.

Adding template parameters using template data in the visual editor. The end result is the same.

You can keep adding parameters like this. For example, let's say we want to use dark theme (theme=Dark) and change the verb (verb=Grateful for). We just use more pipes as separators and code it like this:

{{User loves Coding Help Wiki|color=Purple|theme=Dark|verb=Grateful for}}

And the changes get applied:


Attention template example

Next, let's explore the {{Attention}} template. Unlike the userbox, this template includes some options where you can type whatever you want. By default, it looks like this:

Icon Magnifying Lens LR24
This page is in need of attention.
Please improve this page or discuss this issue on the talk page.

It lets you type reasons why a page might need attention. The parameter is named "reason," and you can put in anything you want. Let's have some fun and make the reason be "simply too beautiful and perfect for human eyes."

{{Attention|reason=simply too beautiful and perfect for human eyes}}

And here's our result:

Icon Magnifying Lens LR24
This page is in need of attention because it may be simply too beautiful and perfect for human eyes.
Please improve this page or discuss this issue on the talk page.

Some templates have constraints on parameter input and some don't. You can always check a template's page to know what you can and can't do with it.

Adding parameters when creating or editing a template

There are two types of parameters: named and unnamed.

A parameter name is wrapped in 3 curly braces. For example, let's say we're making a very simple template and we want to let users choose the text color. We'll name the parameter "textColor." And it'll look like: {{{textColor|}}}

<div style="background-color: white; color: {{{textColor|}}};">
Hello! It's nice to see you!
</div>

The inclusion of the pipe symbol (|) functions like a logical OR. In this case, it means "or nothing" (since nothing is after the pipe). So if users don't specify a text color, then nothing will be there. After all, we don't want our CSS literally saying "color: {{{textColor}}}" when the template gets used.

Default values

We can use the space after the pipe to specify a default value. For example, maybe we'd like to set a default text color in case the user doesn't choose one. To use black as the default, we'd do {{{textColor|black}}}. So we get textColor, or if textColor doesn't exist, we get black.

<div style="background-color: white; color: {{{textColor|black}}};">
Hello! It's nice to see you!
</div>

We can add more parameters. Let's add 3 more:

<div style="background-color: {{{bgColor|white}}}; color: {{{textColor|black}}}; padding: {{{padding|10px}}}; {{{styles|}}}">
Hello! It's nice to see you! {{{message|}}}
</div>

Parser functions

You can use parser functions alongside template parameters for enhanced options. Here's an example of an #if conditional. If the userName parameter gets specified, then it'll include the name in the message. For example, if the name Jane is entered, it'll say "Hello, Jane!" If not, it'll say "Hello!"

<div style="background-color: {{{bgColor|white}}}; color: {{{textColor|black}}}; padding: {{{padding|10px}}}; {{{styles|}}}">
{{#if:{{{userName|}}}|<!-- then -->Hello, {{{userName|}}}! | <!-- else --> Hello!}} It's nice to see you! {{{message|}}}
</div>

Go to our parser functions page to learn more.

Miscellaneous notes

Best practices

Simple, clear names are usually best for your parameters. For example, "textColor" or "list1style" give the user a pretty good guess of what the parameter does.[4]

Add template data to explain your parameters. This is especially helpful when people use the dialog box in the visual editor. Good template data makes templates easy to use.

Use a pipe symbol inside your parameters unless you have a good reason not to.

See also

External links

References