Last modified: 2011-07-29 17:48:40 UTC
It would be great to have a JavaScript variable containing the templates a page uses, similar to the new wgCategories variable. It should be an array with the data presented in the templatesUsed section below the edit box.
It would be better if we stopped using wg* variables. Ideally we would integrate more and more of this sort stuff into the mw object seen in the js2 stuff. mw.page.getCategories(); or mw.page.getTemplates(); for instance... In the mean time, you could use a bit of jQuery like this (on the edit page only of course) var wgTemplatesUsed = jQuery.map( $j( 'div.templatesUsed li a:first-child' ), function( n ) { return $j( n ).text(); } ); - Trevor
I agree that this would be quite good, either as wgTemplates or that stuff Trevor proposes here. However, there are already some options to get them. The API lists them at api.php?action=query&titles=Some_page_name&list=tl or some stuff like that, just check out the documentation if it's wrong If you only want to find out if one template is used, it may be easier to put an id or class within it. For example, at the German Wikipedia, the deletion template uses the id "Vorlage_Löschantragstext". You could then use if (document.getElementById('Vorlage_Löschantragstext')) { stuff to do if there's this template; } else { something else; } However, the latter method of course has the disadvantage that people could just add the id without adding the template, so it's not as stable as the API result or a possible wgTemplates variable.
(In reply to comment #2) > If you only want to find out if one template is used, it may be easier to put > an id or class within it. For example, at the German Wikipedia, the deletion > template uses the id "Vorlage_Löschantragstext". > > You could then use > > if (document.getElementById('Vorlage_Löschantragstext')) { > stuff to do if there's this template; > } > else { > something else; > } > > However, the latter method of course has the disadvantage that people could > just add the id without adding the template, so it's not as stable as the API > result or a possible wgTemplates variable. This is a very bad idea for a general solution, because 1) template names might be legitimate section names or conflict with other IDs and 2) not all template names are valid IDs