Help:Templates

From Source Entities
Jump to navigation Jump to search
Help:Editing

Templates enable you to do things much faster and/or easier. It's like copying and pasting text, but with robust configuration options, and the text can be changed quickly and easily on every page the template is present in. Templates are used very frequently on pages, so it's best to learn about them. When the website sees a template it recognizes on a page, it will automatically replace it with something else. What appears there is up to the editor.

Note: Templates are basically the same as they were on VDC, but a huge amount of optimization has been done, some features got moved around, some templates got moved, etc.

How to use a template[edit source]

Before a template can be added somewhere, first its contents must be defined on its own page. Template pages should start with the prefix Template:.

Templates have some limited programming abilities, which gives them lots of possibilities. We'll talk more about those later.

To add a template to a page (which is called transcluding), simply type {{, the name of the template, and then }}. If you wish to add any parameters, put a | between the template's name and the }}, and define the parameters there. All parameters you define need to be separated by another |.

Examples[edit source]

The simplest type of template is one like Template:Restart. If you typed {{restart}} in the editor on a page, it would be replaced with "Requires restart to take effect.", which is the contents of the page Template:Restart.


Templates can be told to change their contents by being given extra information through parameters. Unnamed parameters are defined in a specific order. Template:cl can accept up to two unnamed parameters.

{{cl|sv_cheats|1}}

Result: sv_cheats 1

Unnamed parameters must be defined in a specific order.


Template:KIO is one of the most common templates used. It accepts an optional named parameter (since).

{{KIO|Script think function|c=thinkfunction|string|Name of a function will be called ten times a second.|since={{l4d2}}}}

Result:

Script think function (thinkfunction) <string> (available in all games since L4d2-small.png)
Name of a function will be called ten times a second.

Without the |since={{l4d2}} at the end, the (available in all games since L4d2-small.png) message would not have displayed. Named parameters can be defined in any order.

Templates can also use templates.

Substitution[edit source]

Instead of transcluding a template, one can choose to substitute it. Substitution will replace a template with the contents of the template directly onto the page. To substitute a template, transclude it like you normally would, then add subst: between {{ and the template's name. After that, click the save button and the wikitext will automatically be added.

Do not substitute carelessly, because the substituted wikitext will not be updated when the template page is edited. See Wikipedia:Help:Substitution for more information.

{{subst:delete|This is a bad page.}}

When the page is saved, that text will be replaced with this:

{{infobox
|bdcolor=red
|bgcolor=white
|txtcolor=black
|image=delete.png
|text=This page has been proposed for deletion.
|small=This is a bad page.}}[[Category:Proposed deletions]]

Note the automatic forwarding of parameters. When substituting, usually the page will not appear any differently than it would if transcluded. The only difference is in the page code.

Advanced substituting[edit source]

If subst: is wrapped in includeonly tags (<includeonly>subst:</includeonly>), whatever is being substituted will only be substituted on the transclusion. This requires that the page as a whole be transcluded, or the subst'ed part will actually display as if it were wrapped in nowiki tags. This technique is used by Template:Date.

Making templates[edit source]

Making a template like Template:Restart is rather easy - just type the text you want, and save it. What about ones with parameters?

Tip: When making templates, you may want to space out your code to make it easier to read, but watch out for unintentional whitespace. HTML comments (<!-- -->) are pretty useful.

Tip: Templates are just that: templates. Sometimes it's better to go off the norm and not use a template.

Unnamed parameters[edit source]

To add an unnamed parameter, simply put {{{1}}} where you want it to be.

Here's (basically) the code for Template:Tip:

Tip: {{{1}}}

If you've seen this template before you know that there's more to it, but this is all we need to look at for now. All the user has to do when transcluding this is enter the text they want:

{{tip|Do not carve without vertex editing afterward.}}

Result: Tip: Do not carve without vertex editing afterward.

If you want to add more than one unnamed parameter, simply change the number. Here's some (condensed) code for Template:KIO:

;{{{1}}} {{#if:{{{2|}}}|<code><[[{{{2}}}]]></code>}}
:{{{3|{{todo2|Description.}}}}}

This template has some things we haven't talked about yet, but you can still see that there's three unnamed parameters used: {{{1}}}, {{{2}}}, and {{{3}}}.

All the user has to do when transcluding is this:

{{KIO|I like letters|string|Words.}}

Result:

I like letters <string>
Words.

Named parameters[edit source]

Named parameters can be useful too as they can be defined in any order, and can be easier to understand, however they do take longer to type. In Template:Oldnote, {{{time}}} marks a place to add wikitext that's defined by |time=texthere when transcluding.

With named parameter:

{{oldnote|time=2006}}

Result: [Noted 2006. Still accurate?]


Without named parameter:

{{oldnote}}

Result: [Noted {{{time}}}. Still accurate?]

Default value[edit source]

To make a parameter have a value by default, add a | to the right of the parameter name, and then define the value. Named and unnamed parameters can use this.

{{{1|3000}}}

This would make the value for {{{1}}}, 3000.

{{{value|}}}

This returns ""; nothing at all. Without the |, it would literally return "{{{value}}}" as text, if it hasn't already been assigned a value at some other point.

Parser Functions[edit source]

#if: checks if a string or parameter has anything inside it.

{{#if: {{{target|}}} | Target aqcuired | Sleep mode activated... }}

Result: Sleep mode activated... (The parameter {{{target|}}} returns null because you (probably) are not viewing this page through a transclusion, so it's never had a chance to be defined.)

{{#if: Something is there! | Target aqcuired | Sleep mode activated... }}

Result: Target aqcuired


#ifeq: checks for equality between two strings. It can be used with parameters as well.

{{#ifeq: {{{target|}}} | friend | Friend... | Go away! }}

Result: Go away!

{{#ifeq: friend | friend | Friend... | Go away! }}

Result: Friend...


#expr: solves math problems.

{{#expr: 2 + 2}}

Result: 4

See m:Help:Calculation for all it's abilities.


#ifexpr: tells if a math expression is correct.

{{#ifexpr: 6 + 3 = 9 | Right | Wrong }}

Result: Right

{{#ifexpr: 6 + 3 = 23 | Right | Wrong }}

Result: Wrong

Documentation[edit source]

Unless the template's usage is inherently easy to understand, please give a description of what your template is, and maybe examples. To keep this describing text from appearing on pages, there are 3 different HTML tags you can use.

noinclude keeps wikitext inside it from appearing on the destination page, while keeping it on the source page.

onlyinclude makes only wikitext inside it appear on destination pages. Text not inside these will still appear on the template's page.

includeonly wikitext inside this will not appear on the template page. It has no effect on the destination page however.

They can sound very confusing at first. If you're confused on how to use them, check the source code for a template, because almost all use them for one purpose or another. If a template's purpose can be understood with a quick glance, skip the documentation. These tags don't need to be closed off like regular HTML tags. Some templates omit </noinclude> at the end of the page to keep the final line return from affecting the appearance.

Templates not updating[edit source]

If you've recently edited a template and the changes are not applying on pages it's been transcluded to, add ?action=purge at the end of the URL for those pages. This will tell the server to update the page's HTML immediately. You can achieve the same effect by editing a page and saving it without making any changes.

Miscellaneous[edit source]

These things are not templates, but are closely related.

__TOC__[edit source]

Forces the table of contents to be placed at this spot.

__FORCETOC__[edit source]

Forces the table of contents to be placed at its normal spot. (Only useful on pages that don't have enough sections to automatically display a TOC.)

__NOTOC__[edit source]

Hides the table of contents.

__HIDDENCAT__[edit source]

Marks a category as hidden. To make the website show hidden categories for you, check the Show hidden categories box in Special:Preferences.

{{DISPLAYTITLE:title}}[edit source]

Changes the title of a page at the top bar only.

{{PAGENAME}}[edit source]

Prints the name of the page.

Note about Module namespace[edit source]

Pages in the Module: namespace are Lua scripts that can do some more advanced things than wikitext templates are capable of. These are allowed on SE, but it would be preferred to avoid them, so that editors don't have to learn Lua just to edit templates.

See Also[edit source]

What's listed on this page is not all that you can use.