Help:Templates

From Valve Developer Community
Revision as of 11:35, 5 July 2022 by Popcorn (talk | contribs) (→‎Parser Functions: Sensivity -> Sensitivity. Named lcfirst: and ucfirst:. Linked mediawiki page about parser functions.)
Jump to navigation Jump to search
English (en)Русский (ru)中文 (zh)Translate (Translate)
Help:Editing

Templates enable you to do things much faster and/or easier. Templates are used very frequently on pages, so it's best to learn about them. When the wiki sees a template it recognizes on a page, it will automatically replace it with something else. What appears there is up to the users.

How to use a Template

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

Note.pngNote:Any page can be transcluded by another one actually. A transclusion of the Main Page can be done using {{:Main Page}}, but we don't do this here because it would add all of the headlines of the Main Page to the table of contents of this page.

Examples

Here's a template where we don't define any parameters, you just drop it right in. If you visit Template:Restart, you'll see that it's just those five words.

{{restart}} → Requires restart to take effect.

The Template:Ent is a template that can accept up to two unnamed parameters. The order of unnamed parameters matters.

{{ent|sv_cheats|1}}sv_cheats 1

Template:KV is one of the most common templates used. It accepts multiple optional named parameters such as only and nofgd. Named parameters can be defined in any order.

{{KV|only={{css}}|ammo|nofgd=1|integer|Amount of reserve ammo to be added. Fallback value is 0.}}
ammo ([todo internal name (i)]) <integer> (only in Counter-Strike: Source) !FGD
Amount of reserve ammo to be added. Fallback value is 0.

Without the |nofgd=1, the !FGD part would not have displayed.

Note.pngNote:Unnamed parameters can be specified like named parameters in any order, like so: {{ent|2=1|1=sv_cheats}}sv_cheats 1
Note.pngNote:When calling a template, you can always specify any number of named and unnamed parameters. The only question is whether the template regards them or not, yielding a different result or the same. By the way, specifying a parameter multiple times displays an error at the top of the page.

Templates can also use templates, named nesting. Recursion is not supported, so a template cannot use itself.

Substitution

This is for advanced users. Instead of transcluding a template, one can choose to substitute it. Substitution will replace the wikitext of a template call with the contents of the template. Do not substitute carelessly, because the substituted wikitext will not be updated when the template is. See Wikipedia:Help:Substitution for more information.

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.

For example, if you write {{restart}} on a page, save it, and edit the page again, you will still find {{restart}} at the same place in the wikitext. In contrast, if you save {{subst:restart}} and edit the page again, you'll find Requires restart to take effect. instead. It can be imagined that the wiki "copy-pastes" the content over, but it does regard parameter values:

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

If a page is saved with this, that text will be replaced with the content of Template:Delete where every occurrence of {{{1}}} is replaced with This is a bad page.:

{{Delete-meta|{{#if: This is a bad page. | This is a bad page. }} }}[[Category:Candidates for speedy deletion]]

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

Tip.pngTip:When editing, be sure to make use of View Changes before saving substitutions to see what you're actually doing to the wikitext.

Making Templates

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

Tip.pngTip: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 (<!-- -->) can also be useful.
Note.pngNote:Named parameters (such as {{{title}}}) will have leading and trailing whitespace trimmed in the template's result, unnamed parameters (such as {{{1}}}) will not.

Unnamed Parameters

To add an unnamed parameter, simply put {{{1}}} where you want the value of the first unnamed parameter that the user might have specified to be inserted. Here's a part of the code for Template:Tip:

Tip:{{{1}}}

If you've seen this template before you know that it has its own special look, but this is all we will 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.}}
Tip.pngTip:Do not carve without vertex editing afterward.

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

:''Not to be confused with [[:{{{1}}}]]{{#if:{{{2|}}} |{{#if:{{{3|}}} |, [[:{{{2}}}]]| or [[:{{{2}}}]].}}|.}}{{#if:{{{3|}}} |{{#if:{{{4|}}} |, [[:{{{3}}}]]|, or [[:{{{3}}}]].}}}}{{#if:{{{4|}}} |, or [[:{{{4}}}]].}}''

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

All the user has to do when transcluding is this:

{{distinguish|Combine|Civil Protection|Overwatch}}
Not to be confused with Combine, Civil Protection or Overwatch.

Named Parameters

Usually you would want to have named parameters affect anything only if they are defined when being transcluded. We'll talk about conditions later. For now, let's look at Template:IO, which has many named parameters. In this template, {{{param}}} marks a place to add wikitext that's defined by |param=texthere when transcluding.

With named parameter:

{{IO|SetString|Updates the string.|param=string}}
SetString <string>
Updates the string.

Without named parameter:

{{IO|SetString|Updates the string.}}
SetString
Updates the string.

Default Value

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

{{{1|3000}}}

This would set the value for 1 to 3000 if the user does not specify it.

{{{value|}}}

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

Parser Functions

To make text (dis)appear only if a certain parameter is defined or set to a specific value, the following expressions are the way to go.

#if: checks if a string is empty or not. The syntax is {{#if: <string> | <then> | <else> }}. This expression evaluates to <then> iff <string> is not the empty string, otherwise to <else>. The <else> value can be omitted in which case it defaults to the empty string; The same applies to the following <else>s.

{{#if: {{{target|}}} | Target aqcuired | Sleep mode activated... }} → Sleep mode activated...
{{#if: Something is there! | Target aqcuired | Sleep mode activated... }} → Target aqcuired
Note.pngNote:The expression {{{target|}}} returns the empty string because you (probably) are not viewing this page through a transclusion, so it's never had a chance to be defined.

#ifeq: checks for equality between two strings. The syntax is {{#ifeq: <string1> | <string2> | <then> | <else> }}. This evaluates to <then> iff the two (trimmed) strings are entirely equal, otherwise to <else>.

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

#switch: checks for equality between an input string and multiple strings. The syntax is {{#switch: <input> | <string1> = <value1> | <string2> = <value2> | ... | #default = <default> }}. This statement evaluates to the corresponsing <valueX> of the first <stringX> that equals the <input>. If that <stringX> is not followed by an equals sign such that there is no <valueX>, then the <valueY> of the next <stringY> that does have an equals sign is used instead. If no equality is found, <default> is returned which defaults to the empty string if not specified.

#expr: solves math problems. See Help:Calculation for all it's abilities.

{{#expr: 2 + 2}} → 4
{{#expr: cos(pi/3)^2}} → 0.25

#ifexpr: tests whether a math expression is true or false and can returns different values depending on it.

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

lc: and uc: convert a string to lower case or upper case. Note that these don't have a hash sign as opposed to #if: and so on. There is also lcfirst: and ucfirst: that only manipulate the first character of the expression.

{{lc:Case Sensitivity}} → case sensitivity
{{uc:Case Sensitivity}} → CASE SENSITIVITY
{{lcfirst:Case Sensitivity}} → case Sensitivity

Documentation

Please give a description of what your template does, and especially examples. To keep this describing text from appearing on pages that use the template, there are three different HTML tags you can use.

  • <noinclude>...</noinclude> makes wikitext inside it not appear on the destination page, while keeping it on the template's page.
  • <onlyinclude>...</onlyinclude> makes only wikitext inside it appear on destination pages. Text not inside these will still appear on the template's page.
  • <includeonly>...</includeonly> makes wikitext inside it not appear on the template page. It has no effect on the destination page.

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 the documentation cannot be kept short, then it is a good idea to move all explanations and/or examples to an own page. By convention, the documentation page of a template should be its subpage /doc. For instance, Template:Note has its documentation page Template:Note/doc. When created, the documentation is transcluded to the template page using just the expression {{documentation}} which of course must be excluded using the above three tags.

Sometimes, templates are used identically. As you might know, Template:Tip and Template:Note both use their one parameter in exactly the same manner, so there is no need for another page with an identical documentation for Tip. Tip can use the same documentation as Note using the expression {{documentation|Template:Note/doc}}; The only magic behind this is that Template:Note/doc uses the expression {{ROOTPAGENAME}} instead of Note for its examples so that the examples adapt to the page that they are displayed on. If Template:Note/doc wasn't made for this, this wouldn't work out. Other "generic docs" like this that are in use are Template:Hl2 and Template:Yes.

Templates not updating

If you've recently edited a template and the changes are not applying on pages that 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. If you're on a template page with a documentation, you can also use the "[purge]" button for the same effect.

Template Shortcuts

Some templates may have template shortcuts, which are redirects that are abbreviated or otherwise different forms of their target templates. Template shortcuts can be transcluded just like their target templates.


Useful Templates

All templates in green text are that color so that they stand out against the greyscale tones of most pages. Most templates with that color also automatically add pages they are transcluded on into Category:TODO.

Per-game Templates

All games have templates named by their shortcut that can deliver their associated icon and/or a link to their page. See Category:Game icons for a full list of game shortcuts. All of them accept the same set of parameters that are described on every template page. The following table should give examples and also give ideas on when to use them.

Wikitext Example Description Formerly used template
{{<shortcut>}} {{hl2}} Half-Life 2 Displays the game's icon which links to its game's page and shows the game's name as a hover text. Putting this at the beginning (or end) of a word or line is the shortest and most common way of clarifying that something applies only to a specific game, but there are more ways.
{{<shortcut>|2}} {{hl2|2}} Half-Life 2 Half-Life 2 Displays the game's icon followed by its name which both link to the game's page. Useful in sentences when the icon alone is too little but the next style is too much. {{game link}}
{{<shortcut>|4}} {{hl2|4}} Half-Life 2 Half-Life 2 Does the same but using colorful text which grabs more attention, so don't overuse this on a page. Especially if you name the same game multiple times in a text, you definitely don't need a long, colorful link every time, let alone a link. {{game name}}
{{<shortcut>|in}} {{hl2|in}} (in Half-Life 2) Can be used to indicate that something applies to a specific game. Useful at the end of a line or key word; The same applies to the following variants. {{in}}
{{<shortcut>|since}} {{portal2|since}} (in all games since Portal 2) Can be used to quickly note features that were added in a game and are available in all Source games afterwards. {{since}}
{{<shortcut>|only}} {{csgo|only}} (only in Counter-Strike: Global Offensive) Indicates features that are exclusive to a specific game. {{only}}
{{<shortcut>|also}} {{as|since}} {{gmod|also}} (in all games since Alien Swarm) (also in Garry's Mod) Notes that earlier game also has a feature, usually backported from newer engine branches. These should be used with in conjunction with another game notice template. {{also}}
{{<shortcut>|removed}} {{l4d|removed}} (removed since Left 4 Dead) Indicates that a feature was removed in a game and all later engine branches. {{removed}}
{{<shortcut>|not}} {{czds|not}} (not in Condition Zero Deleted Scenes) Indicates that a feature might be in other games but not in a specific one. {{not}}


Inline Note Templates

The following are some of the inline note templates.

Description Wikitext and Result
Template:Bug notifies readers of a bug. {{bug|The amount is double what it should be. Be careful!}}
Icon-Bug.pngBug:The amount is double what it should be. Be careful!  [todo tested in ?]
Template:Clarify marks things that should be clarified. PAS is a kind of bounding-box for Player or NPC AI sound detection.{{clarify}}
PAS is a kind of bounding-box for Player or NPC AI sound detection.[Clarify]
Template:Confirm marks something that needs to be confirmed. It's green color is meant to stand out. {{confirm|Always goes to the left.}}
Confirm:Always goes to the left.
Always goes to the left.{{confirm}}
Always goes to the left.[confirm]
Template:Distinguish lets you notify users about pages that share similar names or may be confused for something else. {{distinguish|"Follow Freeman!"|desc1=The HL2 chapter}}
Not to be confused with "Follow Freeman!" (The HL2 chapter).
Template:How asks the question how? Players can get past these.{{how}}
Players can get past these.[How?]
Template:Idea is a nifty way to suggest ideas. {{idea|Reward players for being clever and creative, instead of forcing things to be only one way.}}
Tip.pngIdea:Reward players for being clever and creative, instead of forcing things to be only one way.
Template:Note is for making notes stand out more. {{note|This will spawn at the map origin if you do not specify the position.}}
Note.pngNote:This will spawn at the map origin if you do not specify the position.
Template:Tip marks text as a tip to readers. {{tip|This is a much less tricky process when using the vertex edit tool as well.}}
Tip.pngTip:This is a much less tricky process when using the vertex edit tool as well.
Template:Todo marks things that need to be done. It's green color is meant to stand out. {{todo|Find where this is in the game files.}}
To do: Find where this is in the game files.
Template:Warning marks things that readers should take caution of when doing something. {{warning|This tool has been known to corrupt files. Try an alternative listed below.}}
Warning.pngWarning:This tool has been known to corrupt files. Try an alternative listed below.
Template:Why asks the question why? This shader has been deprecated.{{why}}
This shader has been deprecated.[Why?]


Notice Templates

Description Wikitext and Result
Template:Cleanup marks a page that needs to be fixed up, preferably by someone knowledgeable. The text message is optional. If you do not give sufficient reason for the notice on the transclusion or the talk page, expect it to be removed by someone else. {{cleanup|The last section needs to be clarified.}}
Template:Delete marks a page that should be deleted. If you do not give sufficient reason for the notice on the transclusion or the talk page, expect it to be removed by someone else. When putting this on a page, you should also list the reason on that page's talk page, for any future readers that may want to know the purpose of the deletion. {{delete|This is not a real thing.}}
Template:Merge is a notice that notifies editors that it's been suggested for two or more pages to be combined into one. All merges should be discussed before being done. {{merge|HDR}}
Template:Messagebox provides a standard message box. This template is used by many notice templates, such as merge and cleanup. {{messagebox|text=Remember to follow wiki etiquette.}}
Template:Morescreenshots marks pages that should have more images on them. It allows for an optional text message to clarify what needs images. {{morescreenshots|$envmapcontrast, $envmapsaturation}}
Template:Obsolete marks things that should no longer be used. Accepts three parameters, entity, tool, and shader. {{obsolete|entity=1}}
Template:Spammer marks users that should be banned for spamming. For suspicious but not bannable behavior, there is also Template:Minor Spammer.
Icon-Bug.pngBug:This template's formatting may look weird sometimes. It's intended to be the only thing on a page.  [todo tested in ?]
{{spammer}}
Spammer.jpg
Note.pngNote:This user is a spammer, vandal or other problem poster and should be considered for a ban

Template:Split-apart marks pages that should be split into multiple pages. All splits should be discussed before being done. {{split-apart}}
Template:Stub marks pages that are very small. See Help:Stubs for info on using. {{stub}}
Template:Translate marks pages where translation is needed and adds the page to a category. This template exists in multiple languages, so for a German page, don't add {{translate}} but instead {{translate:de}}. {{translate}}
Template:Update marks outdated pages. {{update}}
Template:Wip marks pages that are in the middle of major edits. The adder requests that other users do not edit in the meantime. {{WIP|~~~}}


Miscellaneous Templates

These templates have the purpose of helping the editor.

Description Wikitext and Result
Template:= is a template that only returns an equal sign. The character is sometimes needed because multiple wiki features make use of it. Especially, the equals sign is a special character in template calls, so if an equals sign should be passed as a parameter value, then {{=}} is needed for "escaping". For the same purpose, there is also Template:! for a pipe, Template:((( for curly brackets and many more, see Category:Templates for within templates. {{=}}
=
{{note|If x {{=}} 0, you lose.}}
Note.pngNote:If x = 0, you lose.
Template:Clr clears the vertical space to the left and right of floating tables, floating images etc. To clear only one side, use {{clr|left}} or {{clr|right}}. This is commonly used before headlines to push them down so that they don't appear next to an image. In the example on the right, one could also use {{clr|left}} for the same effect. [[File:Skull and crossbones.png|50px|left]] This text is next to the image.{{clr}}This text is below the image.

Skull and crossbones.png
This text is next to the image.
This text is below the image.
Template:Ent is a really small template for making text that's both code and a link. {{ent|noclip}}
noclip
Template:Expand or Template:ExpandBox allows you to hide stuff inside a box that can be shrunk down so that it doesn't make pages as long. class="mw-collapsible mw-collapsed" style="background:rgba(0,0,0,0.05) ;float:no;margin-bottom:1em;margin-left:0;padding-right:0.5em;border:1px solid rgba(255,255,255,0.1); border-left:solid 1px #8bb9e0; padding-left:1em;"

|- | {{ExpandBox|{{KV BaseEntity|noscroll=1}}}}


|- | Template:Lang stands for language. This page itself uses the lang template, using the wikitext {{lang|Help:Templates}}. The lang template creates flags at the top right corner of a page such as English Русский 简体中文 and thus must be the first template to be transcluded to be in the right position.

Every page can and should use this template as it lets the reader know of the existence of (correctly titled) language pages of the current page and clicking on the flags links to them. Read more on Template:lang. | {{lang|Help:Templates}}
The result is at the very top of this page.

|- | Template:Param displays the values like parameters in the way that they are used in templates. Useful for template documentations. | {{param|color|#fff}}
{{{color|#fff}}}

|- | Template:Shortcut displays a small box noting (abbreviated) redirects to a page.

| {{shortcut|VDC:AL}}

Shortcut
VDC:AL

|- | Template:Tl stands for Template Link. It creates a link to a template surrounded by {{ }}. | {{tl|note}}
{{note}} |- | Template:Tl2 does the same but using <code> formatting. It also accepts parameters that act like parameters for the template that is linked to. | {{tl2|note|Example}}
{{note|Example}} |- | Template:Tlc does the same but not creating a link, which is useful when trying to display template examples (like this entire page!). | {{tlc|note|Example}}
{{note|Example}} |- | Template:Toc-right shifts the table of contents over to the right side of a page. | For an example, see Half-Life 2 map reference.

|- | Template:Unsigned is for talk pages. It should be added at the ends of unsigned comments. | {{unsigned|JeffLane}}
Unsigned comment added by JeffLane (talkcontribs) Always sign your posts with four tildes (~~~~) |}

Magic Words

Magic words are special character strings that are kind of like templates. To apply one, simply type it on the page.

__TOC__

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

__FORCETOC__

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__

Hides the table of contents.

__HIDDENCAT__

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}}

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

See Also