User blog:Ursuul/Smart Templates: Categories

"I’m lazy. All work I do, I do to reduce the amount of work needing to be done later." Do you hate manually adding categories to your articles? Do you dislike even bothering with categories at all, but still need them to organize your pages? Sit down, then, because with a bit of work you can make such tasks a thing of the past.

MediaWiki software has tons of intuitive functions built in to every Wikia that you can use to program your templates to do everything for you. We will look specifically at how they can be used with your templates to make them categorize pages for you.

Step 1 — Select your template
The best templates for this are templates that are used on many pages & have many parameters; Infoboxes typically. For example, if you have an Infobox that is used on all pages that are “tanks,” & you are trying to organize tank pages by their rank, then that template is the one you will want to use. Template:MYTEMPLATENAME on your Wikia will be the location of the template you have selected.

Step 2 — Identify which parameter should dictate the category
For example, on this page’s infobox, ; this means   is the parameter we want, since we are trying to organize pages by rank. The Categories we are working with are,  , &. We want the Infobox on this page to automatically categorize the page into one of those categories based on what tier has been specified. This is just an example; the parameter could be anything, & you can figure out the parameter you want from a given template in the following ways:

Step 3 — Code
In the Infobox code, we will add something called a switch function. Here is a simple example:

Add that to the very bottom of your chosen template. signifies a template parameter. If the parameter you are targeting is, then   will tell the   to do different things when different values are inserted in the   parameter on the page itself. If, on the page itself, a user sets the tier to, then since   in our switch function, the template will add the   Category to the page! You can add totally different things to the  as well, such as this example:

Advanced
Parser functions such as  are very versatile, & if you throw in some magic words, you can get even more specific functionality.

For example,  is normally case sensitive (to some degree), so if you use   in the template switch function, & you use   on a page, the template will fail to categorize the page properly. But you can teach it to ignore that using this code:

Since the  magic word transforms anything within it to lowercase, you can insert any kind of capitalization into a parameter & it will not matter; it will always be lowercase. For example, if you specify,  , or   as the value on a page, the template will always interpret it as   because   makes it lowercase, so since the value matches   as specified in the  , it will work regardless of capitalization.

This is just one application of a single magic word; there are dozens of magic words with numerous applications, so the opportunities are nearly limitless.

Next Steps
Try this out for yourself. If you were able to apply this principle properly, then you can rinse & repeat until every page on your Wikia contains a template that automatically categorizes it. You can do this for multiple parameters in a given template as well.

Once implemented, the end result is a system of templates that handles all of your categories for you. As an admin, you will never have to worry about fixing, rearranging, or adding categories to pages ever again, since the templates will automatically change their categories based on the information your users add to each page.

It is a bit of work, to be sure, but it could drastically reduce (if not totally eliminate) the amount of work needing to be done later.