Call of Duty Esports Wiki:Tutorial/Templates

Introduction
Templates are the bread and butter of our wiki, and you've seen them all over the place even if you don't realize it -- team, TabsHeader, BlockBox, and scoreboards are all examples of templates. Actually, "template" is a bit of a misnomer - a better name for templates would be "functions." When you make a template, you are putting a bunch of wikitext that you don't want to type out over and over and over again, and making it easily insertable into any place in the wiki. Templates can also reference each other -- for example, TabsHeader references Tabs, which in turn references TabsTop. Templates can vary from very simple to extremely complex.

If you're familiar with programming and/or web design, you'll probably be able to figure out a lot of this on your own, but read over this page and bookmark the help page on parser functions to help you out with syntax anyway. And if you're not, then this page serves as a gentle introduction to templates and what they can do (hint: it's pretty much anything).

Recognizing Templates
How do you know when something is a template? Well, technically any page can be used as a template (more on that later). But in the vast majority of cases, a template will be hosted at a page with a URL like .../wiki/Template:[TemplateName]. When you're editing a page, you can tell that something is a template if it starts with. When the template is over, you will see the closure,.

After the, you will see a word or words, for example   or   or. That word or words is the title of the template, and the same as the rest of the URL after Template: where the template code is located. If it's a simple template, that might be it. But usually there will be at least one variable defined. Let's look at the team template again. If you write, you see. The  |OpTic Gaming  tells the template what champion to use.

Let's look at another template: RosterChangeLine. RosterChangeLine is used on our roster changes tracking page to track rumors. Here's an example line of RosterChangeLine:  Similar to our example above with team, we have the name of the template at the beginning, but this time after the name comes several variables: |date=Rumor, |player=LlamaGod, etc. Each | separates between two variables. In theory, you can have any number of variables in a template.

However, there's another difference that you might notice. In team, we just wrote, not. That's because the champion in team is an unnamed variable, whereas the variables in RosterChangeLine are all named variables.

Using Templates
Now that we can recognize templates and their parts, how do we use them? Some templates, like team will have extensive documentation that you can see when you navigate to their page (remember a template's URL is always .../wiki/Template:[TemplateName]). If you're not sure what to do, seeing if the template in question has documentation can be a great place to start. However, frequently we make templates that don't have documentation -- while this isn't the best practice, we make a lot of on-the-fly templates, and writing documentation for each one would be very time-consuming. So if there's no documentation, how do you figure out what a template does?

The best way is always going to be to look at an example of the template in action. If you're trying to use a template that you don't understand already, chances are that you're editing a page that includes other instances of that template. Most templates will have descriptive names for their variables, or at least you can look at the values that the variables have and figure out what you're supposed to put.

What if you can't find any instances of the template you need to use? Turns out, there's an easy way to look for them! Go to the template's page (Template:TemplateName). Then, find What Links Here under Tools in the sidebar and click it. You'll be taken to a page like this one, where you can see a list of pages that include the template in question. If it says "(transclusion)" after the link, then the template is used in that page. Click to the page that uses the template, go to Edit, and search for the template name within the page.

Still not sure? Let's look at the template code itself. Let's first use a relatively simple example: The code for the tabs used in this tutorial (TutorialTabs).



That looks pretty familiar - in fact, it's just the TabsHeader template with some other stuff added before and after it, and instead of |This=1 or something, it says |This=. In the source code for templates, denotes that something will be a variable when you use the template in another page. There's four things that you can see between tags, and they indicate what type of variable it is:
 * * Technically, what you are doing here, is the same as above with the default value of 2; it's just that the default value is nothing.

Includeonly, Onlyinclude, and Noinclude
Let's go back to our TutorialTabs template, the template we looked at above. What's the deal with it saying   at the start and then    at the end? Here's what's going on.

Fundamentally, templates are no different from any other page in the wiki.
Having a sentence as a section heading is kind of weird, and the only reason I've done it here is that this is a really, really, really important concept. Just because a template is named Template:TemplateName doesn't mean that it's special in any way. When you call a template using  , the wiki assumes that you're looking for something in the Template namespace by default, but if you want you can call something in any namespace!

Remember back when we wrote "Hello, world!" on a page back in Task 1?

It works exactly the same as any other template, inserting code from one place to another.

So! Where do Includeonly, Onlyinclude, and Noinclude come in? Go look at a template called TeamsVsAlign. When you go to the page, you see some documentation about the code, but you don't actually see the code that comprises the template. Why not? What stops you from seeing it, and what stops the documentation from being inserted along with the rest of the template code whenever you use the template?

Transclusion
Transclusion is the inclusion of text from one page to another, accomplished by referencing the one page within the other. You can read about transclusion here. Using a template is the most common form of transclusion.

The meanings of  ,  , and   become clearer if we put them into sentences involving transclusion.
 * Include this code only when transcluding (and not when displaying normally).
 * Only include this part of the code when transcluding.
 * No include this code when transcluding.

Okay, let's get back to TeamsVsAlign. Here's its code:    vs 	

So we have, and then the code that we want it to use, using two variables (unnamed, optional variables), and then a. So all of the code between those two tags is not displayed at Template:TeamsVsAlign but is displayed whenever the code is embedded.

Then after that, we have -- so we're going to display the documentation at Template:TeamsVsAlign, but not when we use the template elsewhere. (What exactly is documentation? We'll go over that in a bit.)

Writing Templates
Templates can range from very simple (such as a template to create one row of tabs, like TutorialTabs, which is used for these pages) to much more complicated (such as RosterChangeLine, the template used to track roster swaps). We'll go over the basics here, and if you want to go more indepth, check out [Help:Templates MediaWiki's help page] and a more in-depth page. You can also contact Chris, our MediaWiki developer.

When should you create a template?

 * When you need to enter a very long piece of code many times with slightly different parameters each time
 * When you need to enter identical code in several places that's subject to change

A good example of the first case is TeamsVsAlign, which was created for the purpose of making the tables in the |Worlds extended stats index for Season World Championship|2014 Worlds. While   vs 	 isn't a ton of code when used once, imagine that much code used for every single table.

The second case applies to pretty much all tournaments that have tabs at the top. There are always multiple pages for the coverage that we have, including Scoreboards, Picks and Bans, etc. And if there's a new tournament added in the series, or if we decide to add additional content, or any other change that needs to be made to the tabs, we don't want to have to change that on every single page -- so we make a template for it.

How to name a template
Please don't name your template anything like "Template1," "GoalieeTV's Template for Worlds Tabs," or "Blueberry Spicehead." Give it a descriptive title that gives users a decent idea of what it's for. TeamsVsAlign is for aligning the "Vs" in a column of "Team 1 vs Team 2" for various teams. CWL 2016 NA Tabs are tabs for the 2016 NA CWL events. TutorialTabs are tabs for the tutorial section. RosterChangeLine generates one row in the table of confirmed changes in our roster swap portal.

Conventions to follow:
 * If your template is primarily a TabsHeader with a variable as the number, make the name ___Tabs, where ___ is a reasonable name for the event or set of pages where the tabs will go.
 * If your template exists to make formatting easier but isn't necessarily going to be used only in the capacity for which you're making it, give it a name based on what it does not why you made it -- TeamsVsAlign rather than WorldsStatsTableEntry is a good example.
 * Multiple words should be combined with a capital at the start of each new entry -- TeamsVsAlign, not Teams_Vs_Align or teamsvsalign.
 * Try to make the name as short as possible without losing information. TeamsVsAlign may not give you a ton of information about what it does, but we don't want something like MakesACellHaveVsInTheCenterAndPutsTeamNamesOnEachSide -- generally a template name should probably be less than 20-25 characters.

Starting your Template
Start out with 

As discussed previously, your code will go in between the includeonly tags. Write the code that you want your template to have, keeping in mind what we went over with variables - you will probably want to have at least one variable in most cases.

Documentation
The last part is documentation. While it's not required, we request that you always document your templates, even if it's something as simple as "Tabs for the Editing Tutorial." (which is the documentation for TutorialTabs). Fortunately, making documentation is very easy, and you already did half of it when you wrote  at the end of your code!

Simply go to .../wiki/Template:TemplateName/doc and write your documentation there, then press save. Then go back to .../wiki/Template:TemplateName and Refresh the page using the button in the dropdown menu next to the search bar. Done!

Your First Template
Let's write a template! Normally, you would put the template at, but since we try to keep that namespace uncluttered, make it at. You'll make a couple versions of the template.

Version 1: No Variable
Make the following template:  Hello, World!

Now make a page called  and write:  You should see "Hello, World!" written.

Version 2: Required Variable
You can choose to make separate templates at, etc, or not. For convenience of writing, we'll assume they're all in the same place. Write the following code:

 My favorite champion is.

Now if you write, you will see "My favorite champion is ."

Version 3: Optional Variable
Same thing, but instead of, try. Now you can write  and it will show "My favorite champion is ."

Version 4: #if
This is slightly more advanced, but still pretty basic. The help article on parser functions should help you understand how the  works.



We've made the same thing as the optional variable above!

These are obviously extremely simple templates, and as you learn more you will make much more complicated ones, using,  , and  , and other parser functions. It can be a lot of fun!