7

Does anybody know how to get access to the template's body inside the page?

I'm familiar with the API that returns the list of ALL templates that exist on the page, but how I can get access to the template's body? Is there any API for this? For now I see only one possible way... parse it manually. Am I wrong?

Roman Snitko
  • 3,655
  • 24
  • 29
Mr. Pumpkin
  • 6,212
  • 6
  • 44
  • 60

2 Answers2

8

You can use the expandtemplates API call, or the rvexpandtemplates parameter for the revisions API call.

Tgr
  • 27,442
  • 12
  • 81
  • 118
  • 1
    and how I can get content of "Template:Infobox writer" on http://en.wikipedia.org/wiki/William_Shakespeare page for example? – Mr. Pumpkin Sep 22 '11 at 13:54
  • The expandtemplates api, when called with `generatexml=1`, transforms wikitext to an XML tree where every template argument is a separate leaf. – Tgr Sep 23 '11 at 10:14
  • Thanks, this is extremely helpful, as it would have been very hard to discover through the API docs. Also note that the two methods mentioned are quite different: `expandtemplates` gives you the rendered (X)HTML as you'd see it on the wiki page, whereas the `revisions` approach gives essentially a direct parse of the template. In both cases, the output is escaped according to the requested `format`. – harpo Feb 21 '14 at 19:45
2

This is an old question, but it helped me figure out how to fetch a mediawiki page with template macros expanded. Very useful if you are doing a conversion.

<MW_BASEURL>/api.php?action=query&prop=revisions
   &titles=<url_encoded_page_title>&format=xml&rvprop=content&rvexpandtemplates

I am parsing the xml returned from this query to get the expanded page.

russtman
  • 153
  • 2
  • 6
  • Thanks very much! The current implementation of mediawiki does not take care of templates well. I hope they can fix it in the next version. – Gang Liang Mar 02 '15 at 19:46