Tutorial/Templates

Template:Community Tabs

## 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 -- `{{ci}}`, `{{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 *.../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 `TabsHeader` or `SetPlayerVariables` or `Infobox Team`. 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 `{{ci}}` template again. If you write {{ci|Sona}}, you see Template:Ci. The **|Sona** tells the template what champion to use.

Let's look at another template: `{{RosterSwapLine}}`. `{{RosterSwapLine}}` is used on our roster changes tracking page to track rumors. Here's an example line of `{{RosterSwapLine}}`:

```
```

Similar to our example above with `{{ci}}`, we have the name of the template at the beginning, but this time after the name comes several variables: |date=Rumor, |player=Chaox, 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 `{{ci}}`, we just wrote `{{ci|Sona}}`, not `{{ci|champ=Sona}}`. That's because the champion in `{{ci}}` is an **unnamed** variable, whereas the variables in `{{RosterSwapLine}}` are all **named** variables.

 Must always be written in the exact same order, if there are multiple unnamed variables\*
 Will **usually** be used only in templates with at most 2-3 variables

## Using Templates

Now that we can recognize templates and their parts, how do we use them? Some templates, like `{{ci}}` will have extensive documentation that you can see when you navigate to their page (remember a template's URL is always *....com/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.

## Includeonly, Onlyinclude, and Noinclude

What's the deal with it saying *<includeonly>* at the start and then *</includeonly><noinclude>{{documentation}}</noinclude>* at the end? Here's what's going on.

### Fundamentally, templates are no different from any other page in the wiki.

Just because a template is named *Template:TemplateName* doesn't mean that it's special in any way. When you call a template using *{{templatename}}*, 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?

```
{{User:RheingoldRiver/Tutorial_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 *<includeonly>*, *<onlyinclude>*, and *<noinclude>* 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.

 <includeonly>
 <onlyinclude>
 <noinclude>

Okay, let's get back to `{{TeamsVsAlign}}`. Here's its code:

```
<includeonly><div style="width:{{{width|300px}}};display:table;table-layout:fixed;">
	<div style="width:calc(50% - 10px);text-align:right;display: table-cell;"> {{team|{{{1|}}}|leftshortlinked}} </div>
	<div style="display:table-cell;width:20px;text-align:center"> vs </div>
	<div style="width:calc(50% - 10px);display:table-cell;text-align:left;">  {{team|{{{2|}}}|rightshortlinked}} </div>
</div></includeonly><noinclude>{{documentation}}</noinclude>
```

So we have <includeonly>, and then the code that we want it to use, using two variables (unnamed, optional variables), and then a </includeonly>. 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 <noinclude>{{documentation}}</noinclude> -- 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 2014 Worlds. While

```
<div style="width:{{{width|300px}}};display:table;table-layout:fixed;">
	<div style="width:calc(50% - 10px);text-align:right;display: table-cell;"> {{team|{{{1|}}}|leftshortlinked}} </div>
	<div style="display:table-cell;width:20px;text-align:center"> vs </div>
	<div style="width:calc(50% - 10px);display:table-cell;text-align:left;">  {{team|{{{2|}}}|rightshortlinked}} </div>
</div>
```

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," "RheingoldRiver'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. `{{Season 3 NA LCS Tabs}}` are tabs for the season 3 NA LCS 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

```
<includeonly>
</includeonly><noinclude>{{Documentation}}</noinclude>
```

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 `<noinclude>{{Documentation}}</noinclude>` at the end of your code!

Simply go to *Template:TemplateName/doc* and write your documentation there, then press save. Then go back to *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 `Template:[Template Name]`, but since we try to keep that namespace uncluttered, make it at `User:[Username]/My_First_Template`. You'll make a couple versions of the template.

### Version 1: No Variable

Make the following template:

```
<includeonly>Hello, World!</includeonly>
```

Now make a page called `User:[Username]/TemplateTest` and write:

```
{{[Username]/My_First_Template}}
```

You should see "Hello, World!" written.

### Version 2: Required Variable

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

```
<includeonly>My favorite champion is {{ci|{{{1}}}}}.</includeonly>
```

Now if you write `{{My_First_Template|Sona}}`, you will see "My favorite champion is Template:Ci."

### Version 3: Optional Variable

Same thing, but instead of `{{{1}}}`, try `{{{1|Sona}}}`. Now you can write `{{My_First_Template}}` and it will show "My favorite champion is Template:Ci."

### Version 4: #if

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

```
<includeonly>{{#if: {{{1|}}}
```

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 `#ifeq`, `#switch`, and `#expr`, and other parser functions. It can be a lot of fun!

Template:CommunityNavbox