API:Templates

MediaWiki version:
1.11

GET request to get a list of all pages (typically templates) transcluded in the provided pages.

API documentation

prop=templates (tl)

(main | query | templates)
  • This module requires read rights.
  • This module can be used as a generator.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Returns all pages transcluded on the given pages.

Specific parameters:
Other general parameters are available.
tlnamespace

Show templates in these namespaces only.

Values (separate with | or alternative): -1, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 710, 711, 828, 829, 1198, 1199, 2600, 5500, 5501
To specify all values, use *.
tllimit

How many templates to return.

Type: integer or max
The value must be between 1 and 500.
Default: 10
tlcontinue

When more results are available, use this to continue. More detailed information on how to continue queries can be found on mediawiki.org.

tltemplates

Only list these templates. Useful for checking whether a certain page uses a certain template.

Separate values with | or alternative.
Maximum number of values is 50 (500 for clients that are allowed higher limits).
tldir

The direction in which to list.

One of the following values: ascending, descending
Default: ascending
Examples:
Get the templates used on the page MediaWiki.
api.php?action=query&prop=templates&titles=MediaWiki [open in sandbox]
Get information about the template pages used on the page MediaWiki.
api.php?action=query&generator=templates&titles=MediaWiki&prop=info [open in sandbox]
Get pages in the User and Template namespaces that are transcluded on the page MediaWiki.
api.php?action=query&prop=templates&titles=MediaWiki&tlnamespace=2|10 [open in sandbox]


Example

GET request

Get a list of templates used on a page.

Response

{
    "continue": {
        "tlcontinue": "736|10|Birth_date",
        "continue": "||"
    },
    "query": {
        "pages": {
            "736": {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "templates": [
                    {
                        "ns": 0,
                        "title": "Einstein"
                    },
                    {
                        "ns": 10,
                        "title": "Template:20th Century Press Archives"
                    },
                    ...
                ]
            }
        }
    }

Sample code

Python

#!/usr/bin/python3

"""
    templates.py
    MediaWiki API Demos
    Demo of `Templates` module: Get a list of templates used on a page
    MIT License
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    "action": "query",
    "titles": "Albert Einstein",
    "prop": "templates",
    "format": "json"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

PHP

<?php

/*
    templates.php
    MediaWiki API Demos
    Demo of `Templates` module: Get a list of templates used on a page
    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "titles" => "Albert Einstein",
    "prop" => "templates",
    "format" => "json"
];

$url = $endPoint . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

$result = json_decode( $output, true );
var_dump( $result );

JavaScript

/*
    templates.js
    MediaWiki API Demos
    Demo of `Templates` module: Get a list of templates used on a page
    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    titles: "Albert Einstein",
    prop: "templates",
    format: "json"
};

url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});

fetch(url)
    .then(function(response){return response.json();})
    .then(function(response) {console.log(response);})
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	templates.js
	MediaWiki API Demos
	Demo of `Templates` module: Get a list of templates used on a page
	MIT License
*/

var params = {
		action: 'query',
		titles: 'Albert Einstein',
		prop: 'templates',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	console.log( data );
} );


Parameter history

  • v1.19: Introduced tldir
  • v1.17: Introduced tltemplates
  • v1.x: Introduced tlcontinue, tllimit

Additional notes

See also