Topic on Project:Support desk

Use ResourceLoader in parser extension?

2
Subfader (talkcontribs)

Can I add RL modules in a parser extension?

function pageMcJdTlRenderParserFunction( $parser, $max="10", OutputPage $out ) {
	$wgResourceModules['MyExt.explorerJs'] = array(
		'scripts' => 'modules/MyExt.explorer.js',
		//'styles'  => 'modules/MyExt.explorer.css',
		'localBasePath' => __DIR__,
		'remoteExtPath' => '/',
	);
	$out->addModules( 'MyExt.explorerJs' );
	
	return "test";
}
TheDJ (talkcontribs)

Yes, something like SyntaxHighlight does that for instance.. But, if they are parser output dependent, then you need to add them to the parseroutput, and not to the outputpage.

$parser->getOutput()->addModules()

(Parseroutput is cached, thus if your page view would be rerendered, without a new parseroutput being generated, as you have it there, it wouldn't know to re-add the modules).

BTW. you should define your resourceloader modules from extension.json these days, or at least outside of the the parserfunction, since a parser function is run for each parsercall (so for 100 calls to the function on a page, it would redefine the resourceloader module 100 times in your current example).