Applying a template to all pages of a given category

From FollowTheScore
Jump to: navigation, search

How can one apply a template to all pages of a certain category?

Sometimes you want to create a list of articles where each entry in the list points to an adress which is constructed from the article´s name. Basically, this means that you want to produce the equivalent of

   {{some template|1st page in category}}
   {{some template|2nd page in category}}
   {{some template|3rd page in category}}
   ...

Answer

The way how to do this depends slightly on the mode you use for your DPL call.

Variant 1: function call with <dpl> tag

To invoke a template called "My special article lister" for all pages which are members of category "Fictitious country" you would have to write the following:

<dpl>
   category=Fictitious country
   format=,{{My special article lister|%PAGE%}}
</dpl>

The result could like this:


Variant 2a (parser function call, all MW versions)

If you want to use parser function syntax there are some differences: If you are using MediaWiki 1.11 or below you cannot use double curly braces or pipe symbols within the dpl call directly. Instead you must use special escape marks for these symbols. Note the broken pipe symbol and the little ² around the single curly braces.

{{#dpl:
  |category=Fictitious country
  |format=,²{My special article lister¦%PAGE%}²
}}

The result looks the same as before:


Variant 2b (parser function call, only MW 1.11 or above)

If you are using MediaWiki 1.11 or above you can use the normal symbols within the DPL text. Because of a parser change MediaWiki does no longer expand those symbols before it invokes DPL. Instead DPL does this for you after it has done its job. This leads - in principle - to the desired behaviour. But there is still a minor difference: Leading white space will be stripped from the template and there is newline between the template invocations. Therefore the asterisk in column 1 of the template will not be recognized as bulleted list markup. So you must insert that newline symbol before invoking the template. DPL provides the \n for that purpose:

{{#dpl:
  |category=Fictitious country
  |format=,\n{{My special article lister|%PAGE%}}
}}

The result still looks the same (note that we are using MW 1.13 (or above) in this wiki.


Further possibilities

Of course you need not construct a bulleted list within the template DPL calls for you. You can do whatever you want. You can have #if-expressions which suppress some articles, you can add images (based on the article#s name) etc. You can also pass the %TITLE% in addition to the %PAGE% variable to your template.

Your template could even contain a second DPL query which looks into the content of the article and extracts some useful information. But be careful:

  • This may increase resource consumption significantly
  • There are more efficient ways to do this. See the include statement.