User:Willscrlt/commons/Category suppression in templates (en)

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Many templates created by Willscrlt and other users include an optional attribute of "categories=no". This article explains what it is, how it works, and how to incorporate its features in other templates.

Note: Some templates use |nocat=true or |category=  for the same purpose. Please check the template's documentation to see which method is supported.

Problem[edit]

Many templates automagically add the pages on which they appear to certain categories. There are many reasons that templates might do this. Userboxes add users to various Wikipedian categories based on interests. WikiProject headers add talk pages to project categories. Infoboxes sometimes add articles to topical categories. Other templates flag articles for certain actions by adding the articles to administrative categories.

This can be a really useful and powerful feature, but it can also be a little confusing when someone wants to display the template, such as in a template gallery or a help page. The gallery or help page itself is flagged like any other page on which the template appears, and this is often not desirable. It is unlikely that a page demonstrating a deletion template should be flagged for deletion itself. Also, users may wish to place the same userbox on more than one of their pages, but they probably do not wish to have each page be listed multiple times in the related categories.

Within the template itself, it is fairly easy to suppress such categorization problems using the <includeonly> and <noinclude> markup. However, those features do not help when displaying the template as an example.

Solution[edit]

At the expense of a little extra processing on the server and making the template code a little bit more difficult for humans to read, a simple parser function can suppress this feature by not displaying the categories when a particular template attribute is set to "no".

The standard code for templates to include a category looks like this (where you set Example to the actual category name):

<includeonly>[[Category:Example|{{PAGENAME}}]]</includeonly>

To add the |categories=no feature, just add the code shown in bold:

<includeonly>{{#ifeq:{{{categories}}}|no||[[Category:Example|{{PAGENAME}}]]}}</includeonly>
  • The {{#ifeq:{{{categories}}}|no is a parser function that basically asks the question, "Was an attribute named categories passed to this template with the value set to no?"
  • The next part, ||, displays nothing (i.e., it suppresses the category) if the answer to that question is true.
  • The rest is the code normally used to display the category, but it only appears if the answer to the question is false (i.e., there was not such an attribute set to no).
  • The final }} ends the parser function (it's very important to include this, or else the template will act very strangely!).

Implementation[edit]

To add this feature to any template, just sandwich the current category tag between {{#ifeq:{{{categories}}}|no|| and }} as shown above.

It is also a good idea to add <noinclude>[[Category:Category suppression-supporting templates|{{PAGENAME}}]]</noinclude> to the template itself to identify the template as such.

Usage[edit]

Anyplace that the template should be displayed without adding the page to categories, just add |categories=no as the last attribute for the template, and (assuming the template is implemented correctly), the template will not add the page to any categories.

Note: Some templates use |nocat=true or |category=  for the same purpose. Please check the template's documentation to see which method is supported.